Given a linked list and an integer n, append the last n elements of the LL to front. Assume given n will be smaller than length of LL.
Input format: Line 1: Linked list elements (separated by space and terminated by -1
Sample Input 1 :
1 2 3 4 5 -1
3
Sample Output 1 :
3 4 5 1 2
Description:
The question asks us to append the last N nodes to front, i.e the new linked list should first start from those N nodes and then traverse the rest of the nodes through the head of the old linked list.
Example:
For Linked List 1->2->3->4->5->6->NULL
To append the last 2 nodes, the new linked list should be:
5->6->1->2->3->4->NULL
Algorithm:
Steps:
Function:
C++ Code:
Output