You are viewing a single comment's thread. Return to all comments →
SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { if (llist != NULL) { SinglyLinkedListNode *before, *tempList, *newNode = new SinglyLinkedListNode(data); tempList = llist; for (int i = 1; i < position; i++) {tempList = tempList->next;} newNode->next = tempList->next; tempList->next = newNode; return llist; } else { return new SinglyLinkedListNode(data);} }
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) { if (llist != NULL) { SinglyLinkedListNode *before, *tempList, *newNode = new SinglyLinkedListNode(data); tempList = llist; for (int i = 1; i < position; i++) {tempList = tempList->next;} newNode->next = tempList->next; tempList->next = newNode; return llist; } else { return new SinglyLinkedListNode(data);} }