Created
January 17, 2022 17:12
-
-
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.
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
/* | |
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