Created
May 9, 2016 21:10
-
-
Save jianminchen/957d2025fd310a9e59fb88fe8766e0de to your computer and use it in GitHub Desktop.
HackerRank - LinkedList - insert a node at tail of linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Insert Node at the end of a linked list | |
head pointer input could be NULL as well for empty list | |
Node is defined as | |
struct Node | |
{ | |
int data; | |
struct Node *next; | |
} | |
*/ | |
Node* Insert(Node *head,int data) | |
{ | |
Node* newNode = new Node(); | |
newNode->data = data; | |
if(head==NULL) | |
return newNode; | |
Node* nextNode = Insert(head->next, data); | |
head->next = nextNode; | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment