Skip to content

Instantly share code, notes, and snippets.

@thuvh
Created September 20, 2018 16:23
Show Gist options
  • Save thuvh/a7bb2f8982743e4397267ad3190ef04f to your computer and use it in GitHub Desktop.
Save thuvh/a7bb2f8982743e4397267ad3190ef04f to your computer and use it in GitHub Desktop.
insertnodeattail
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode* tail = new SinglyLinkedListNode(data);
if (head == nullptr){
return tail;
} else {
SinglyLinkedListNode* tmp = head;
while(tmp->next != nullptr){
tmp = tmp->next;
}
tmp->next = tail;
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment