Given a singly linked list and an integer K. The task is to append last K elements of the linked list to front.
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6, k = 3
Output : 4 -> 5 -> 6 -> 1 -> 2 -> 3
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6, k = 7
Output : 6 -> 1 -> 2 -> 3 -> 4 -> 5
Prerequisites: Move last element to front of a given Linked List
- Loop over (k % n) times, Where n is the number of elements of the linked list.
- Each time, delete one node from the end of the linked list.
- Simultaneously insert that deleted node at the beginning of the linked list.
- Move last element to front of a given Linked List
- Move last m elements to the front of a given Linked List
- Move all zeros to the front of the linked list
- Self Organizing List : Move to Front Method
- Move first element to end of a given Linked List
- Move all occurrences of an element to end in a linked list
- Create new linked list from two given linked list with greater element at each node
- Rearrange a linked list in to alternate first and last element
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Find the sum of last n nodes of the given Linked List
- Find the product of last N nodes of the given Linked List
- Append the last M nodes to the beginning of the given linked list
- Create a linked list from two linked lists by choosing max element at each position
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Delete last occurrence of an item from linked list
Below is the implementation of the above approach :
Linked List before appending: 1 2 3 4 5 6 Linked List after appending 7 elements: 6 1 2 3 4 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.