You are viewing a single comment's thread. Return to all comments →
Another node assignment is not required;
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
SinglyLinkedListNode headNode = head; SinglyLinkedListNode newNode = new SinglyLinkedListNode(data); int pos = 0; while(headNode.next != null && pos < position - 1){ headNode = headNode.next; pos++; } SinglyLinkedListNode nodeAtPosition = headNode.next; headNode.next = newNode; newNode.next = nodeAtPosition; 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 →
Another node assignment is not required;
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {