A linked list L0 -> L1 -> L2 -> ….. -> LN can be folded as L0 -> LN -> L1 -> LN – 1 -> L2 -> …..
Given a folded linked list, the task is to unfold and print the original linked list
Input: 1 -> 6 -> 2 -> 5 -> 3 -> 4
Output: 1 2 3 4 5 6
Input: 1 -> 5 -> 2 -> 4 -> 3
Output: 1 2 3 4 5
Approach: Make a recursive call and store the next node in temp pointer, first node will act as head node and the node which is stored in temp pointer will act as a tail of the list. On returning after reaching the base condition link the head and tail to previous head and tail respectively.
Base condition: If number of nodes is even then the second last node is head and the last node is tail and if the number of nodes is odd then last node will act as head as well as tail.
Below is the implementation of the above approach:
1 5 2 4 3 1 2 3 4 5
- Program for n'th node from the end of a Linked List
- Java Program for Reverse a linked list
- C Program for Bubble Sort on Linked List
- Program to reverse a linked list using Stack
- C Program to reverse each node value in Singly Linked List
- Program to find average of all nodes in a Linked List
- Program to find size of Doubly Linked List
- Python program to find middle of a linked list using one traversal
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and Doubly linked list
- Convert singly linked list into circular linked list
- Merge a linked list into another linked list at alternate positions
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Convert Singly Linked List to XOR Linked List
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.
Improved By : princiraj1992