Insert a node at a specific position in a linked list:
"""
Insert Node at a specific position in a linked list
head input could be None as well for empty list
Node is defined as
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
return back the head of the linked list in the below method.
"""
def InsertNth(head, data, position):
if head == None:
return Node(data,None)
curr_node = head
if position == 0:
return Node(data,curr_node)
while position!=1:
curr_node = curr_node.next
position -=1
x = Node(data,curr_node.next)
curr_node.next = x
return head