You are viewing a single comment's thread. Return to all comments →
SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data); if(position == 0) { newNode->next = llist; return newNode; } SinglyLinkedListNode* curr = llist; while(position - 1) { position--; curr = curr -> next; } newNode -> next = curr->next; curr->next = newNode; 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 →
SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data); if(position == 0) { newNode->next = llist; return newNode; } SinglyLinkedListNode* curr = llist; while(position - 1) { position--; curr = curr -> next; } newNode -> next = curr->next; curr->next = newNode; return llist; }