You are viewing a single comment's thread. Return to all comments →
JavaScript solution
function insertNodeAtPosition(llist, data, position) { class Node { constructor(value) { this.data = value; this.next = null; } } let newNode = new Node(data); let length = position; let current = llist; let prev; if (position === 0 && llist) { prev = current; current = current.next; } else { while (length) { prev = current; current = current.next; length--; } } prev.next = newNode; newNode.next = current; return llist; }
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 →
JavaScript solution