You are viewing a single comment's thread. Return to all comments →
Java Solution
public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) { SinglyLinkedListNode head = llist; SinglyLinkedListNode current = llist; SinglyLinkedListNode prevNode = null; int count = 0; while (current != null) { if (count == position) { SinglyLinkedListNode insNode = new SinglyLinkedListNode(data); insNode.next = current; if (prevNode == null) head = insNode; else prevNode.next = insNode; break; } prevNode = current; current = current.next; count++; } return head; }
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 →
Java Solution