Last active
August 15, 2020 14:29
-
-
Save omonimus1/ba3cd6c09936aab166378a86c9e51022 to your computer and use it in GitHub Desktop.
Having in input a reference to the head of a linked list and value x, store the value x in a new node and set this node as new tails of the 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
Node push_tail(Node *head, int x) | |
{ | |
Node *new_tail = new Node(x); | |
new_tail->next = NULL; | |
if(head == NULL) | |
return new_tail; | |
else | |
{ | |
Node *current =head; | |
// Iterate until the last node | |
while(current->next != NULL) | |
current = current->next; | |
// Set last_node next's pointer pointing to the new_tail | |
current->next =new_tail; | |
return head; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment