Advertisement

Insert a node at a specific position in a linked list

Insert a node at a specific position in a linked list This video is about Insert a node at a specific position in a linked list.

Problem Description:
You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node.
A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.
As an example, if your list starts as 1-2-3 and you want to insert a node at position 2 with data=4, your new list should be 1-2-4-3

Problem:


Code Sample:
def insertNodeAtPosition(head, data, position):
new=SinglyLinkedListNode(data)
if position==0:
new.next=head
head=new
return head
ptr=head
c=1
while ptr.next is not None:
if c== position:
new.next=ptr.next
ptr.next=new
break
c+=1
ptr=ptr.next
return head

Linear Linked List in detail
Part-1.
Part-2.
Part-3.
Part-4.

Insert a Node,Insert a node at a specific position in a linked list,linked list,data structure,linklist in python,interview,insert a node at a specific position in a linked list hackerrank solution,nsert a node at a specific position in a linked list hackerrank solution python,insert a node at a specific position in a linked list hackerrank,how to insert a node at a specific position in a linked list,insert a node at the head of a linked list hackerrank solution,

Post a Comment

0 Comments