Skip to content

Instantly share code, notes, and snippets.

@omonimus1
Last active August 15, 2020 14:29
Show Gist options
  • Save omonimus1/ba3cd6c09936aab166378a86c9e51022 to your computer and use it in GitHub Desktop.
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
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