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.
publicstaticSinglyLinkedListNodeinsertNodeAtPosition(SinglyLinkedListNodellist,intdata,intposition){SinglyLinkedListNodenewNode=newSinglyLinkedListNode(data);// If inserting at the head (position 0)if(position==0){newNode.next=llist;returnnewNode;}// Traverse the list to find the node before the desired positionSinglyLinkedListNodecurrent=llist;for(inti=0;i<position-1&¤t!=null;i++){current=current.next;}// Insert the new nodeif(current!=null){newNode.next=current.next;current.next=newNode;}returnllist;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insert a node at a specific position in a linked list
You are viewing a single comment's thread. Return to all comments →