We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
definsertNodeAtPosition(llist,data,position):# Create the new node with the given datainserted_node=SinglyLinkedListNode(data)# If inserting at the head (position 0)ifposition==0:inserted_node.next=llistreturninserted_node# Traverse the list to find the insertion pointcurr=llistcurr_pos=1# Loop until the position before the insertion pointwhilecurr_pos!=position:curr=curr.nextcurr_pos+=1# Insert the new node at the given positioninserted_node.next=curr.nextcurr.next=inserted_nodereturnllist
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Insert a node at a specific position in a linked list
You are viewing a single comment's thread. Return to all comments →