Program to unfold a folded linked list
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
Examples:
Input: 1 -> 6 -> 2 -> 5 -> 3 -> 4
Output: 1 2 3 4 5 6Input: 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:
Java
// Java implementation of the approach public class GFG { // Node Class private class Node { int data; Node next; } // Head of the list private Node head; // Tail of the list private Node tail; // Function to print the list public void display() { if (head == null) return; Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } // Funcion to add node in the list public void push(int data) { // Create new node Node nn = new Node(); nn.data = data; nn.next = null; // Linking at first position if (head == null) { head = nn; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } // Linking at last in list temp.next = nn; } } // Function to unfold the given link list private void unfold(Node node) { if (node == null) return; // This condition will reach if // the number of nodes is odd // head and tail is same i.e. last node if (node.next == null) { head = tail = node; return; } // This base condition will reach if // the number of nodes is even // mark head to the second last node // and tail to the last node else if (node.next.next == null) { head = node; tail = node.next; return; } // Storing next node in temp pointer // before making the recursive call Node temp = node.next; // Recursive call unfold(node.next.next); // Connecting first node to head // and mark it as a new head node.next = head; head = node; // Connecting tail to second node (temp) // and mark it as a new tail tail.next = temp; tail = temp; tail.next = null; } // Driver code public static void main(String[] args) { GFG l = new GFG(); // Adding nodes to the list l.push(1); l.push(5); l.push(2); l.push(4); l.push(3); // Displaying the original nodes l.display(); // Calling unfold function l.unfold(l.head); // Displaying the list // after modification l.display(); } } |
C#
// C# implementation of the approach using System; public class GFG { // Node Class private class Node { public int data; public Node next; } // Head of the list private Node head; // Tail of the list private Node tail; // Function to print the list public void display() { if (head == null) return; Node temp = head; while (temp != null) { Console.Write(temp.data + " "); temp = temp.next; } Console.WriteLine(); } // Funcion to add node in the list public void push(int data) { // Create new node Node nn = new Node(); nn.data = data; nn.next = null; // Linking at first position if (head == null) { head = nn; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } // Linking at last in list temp.next = nn; } } // Function to unfold the given link list private void unfold(Node node) { if (node == null) return; // This condition will reach if // the number of nodes is odd // head and tail is same i.e. last node if (node.next == null) { head = tail = node; return; } // This base condition will reach if // the number of nodes is even // mark head to the second last node // and tail to the last node else if (node.next.next == null) { head = node; tail = node.next; return; } // Storing next node in temp pointer // before making the recursive call Node temp = node.next; // Recursive call unfold(node.next.next); // Connecting first node to head // and mark it as a new head node.next = head; head = node; // Connecting tail to second node (temp) // and mark it as a new tail tail.next = temp; tail = temp; tail.next = null; } // Driver code public static void Main() { GFG l = new GFG(); // Adding nodes to the list l.push(1); l.push(5); l.push(2); l.push(4); l.push(3); // Displaying the original nodes l.display(); // Calling unfold function l.unfold(l.head); // Displaying the list // after modification l.display(); } } /* This code contributed by PrinciRaj1992 */ |
1 5 2 4 3 1 2 3 4 5
Recommended Posts:
- Program for n'th node from the end of a Linked List
- C Program for Bubble Sort on Linked List
- Java Program for Reverse a linked list
- Program to reverse a linked list using Stack
- Program to find average of all nodes in a Linked List
- Program to find size of Doubly Linked List
- C Program to reverse each node value in Singly 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
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Difference between Singly linked list and Doubly linked list
- Check if a linked list is Circular 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 contribute@geeksforgeeks.org. 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



