Append last K nodes of a Linked List
Initial list: 1->2->3->4->5->6->null

Append 3 nodes

Final List: 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;
}
Comments (1)