Skip to content

Instantly share code, notes, and snippets.

@Kanav-Arora
Created January 17, 2022 17:12
Show Gist options
  • Save Kanav-Arora/5658a822e7a1377270417682d887abdd to your computer and use it in GitHub Desktop.
Save Kanav-Arora/5658a822e7a1377270417682d887abdd to your computer and use it in GitHub Desktop.
Append last k nodes of a given linked list in start of list.
/*
Initial list: 1->2->3->4->5->6->null
Append 3 nodes
4->5->6->1->2->3->null
*/
void appendKNodes(Node* &head, int k)
{
Node* temp = head;
for(int i=0; i<length(head)-k-1; i++)
{
temp = temp->next;
}
Node* new_tail = temp;
Node* new_head = temp->next;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = head;
new_tail->next = NULL;
head = new_head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment