You are viewing a single comment's thread. Return to all comments →
Even I had a similar idea. Did your code pass all the test cases?
Node InsertNth(Node head, int data, int position) {
Node newNode = new Node(); newNode.data = data; int count = 1; if(position == 1) { if(head == null) { head = newNode; newNode.next = null; } else { newNode.next = head; head = newNode; } } else { Node current = head; while(count<position - 1 || current.next!=null) { current = current.next; count++; } newNode.next = current.next; current.next = newNode; } 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 →
Even I had a similar idea. Did your code pass all the test cases?
Node InsertNth(Node head, int data, int position) {
}