You are viewing a single comment's thread. Return to all comments →
// Insert a node at a specific position in a linked list // JAVA Solution
if(llist == null){ if(position==1) return new SinglyLinkedListNode(data); } if(position==1){ SinglyLinkedListNode temp = new SinglyLinkedListNode(data); return temp; } SinglyLinkedListNode curr = llist; int count = 0; while(curr != null){ if(count == position-1){ SinglyLinkedListNode temp = new SinglyLinkedListNode(data); temp.next = curr.next; curr.next = temp; break; } count++; curr = curr.next; } 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 →
// Insert a node at a specific position in a linked list // JAVA Solution
if(llist == null){ if(position==1) return new SinglyLinkedListNode(data); } if(position==1){ SinglyLinkedListNode temp = new SinglyLinkedListNode(data); return temp; } SinglyLinkedListNode curr = llist; int count = 0; while(curr != null){ if(count == position-1){ SinglyLinkedListNode temp = new SinglyLinkedListNode(data); temp.next = curr.next; curr.next = temp; break;
} count++; curr = curr.next; } return llist;