You are viewing a single comment's thread. Return to all comments →
Much too long, you can simplify this by not iterating when you don't need to.
Node newNode = new Node(); newNode.data = data; Node current = head; if( position == 0 ){ newNode.next = head; return newNode; } int counter = 0; while( ++counter < position ){ current = current.next; } newNode.next = current.next; current.next = newNode; return head;
Insert a node at a specific position in a linked list
You are viewing a single comment's thread. Return to all comments →
Much too long, you can simplify this by not iterating when you don't need to.