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 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:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
Node *head;
Node *tail;
void display()
{
if (head == NULL)
return ;
Node* temp = head;
while (temp != NULL)
{
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
void push( int data)
{
Node* nn = new Node();
nn->data = data;
nn->next = NULL;
if (head == NULL)
{
head = nn;
}
else
{
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = nn;
}
}
void unfold(Node* node)
{
if (node == NULL)
return ;
if (node->next == NULL)
{
head = tail = node;
return ;
}
else if (node->next->next == NULL)
{
head = node;
tail = node->next;
return ;
}
Node* temp = node->next;
unfold(node->next->next);
node->next = head;
head = node;
tail->next = temp;
tail = temp;
tail->next = NULL;
}
int main()
{
push(1);
push(5);
push(2);
push(4);
push(3);
display();
unfold(head);
display();
}
|
Java
public class GFG {
private class Node {
int data;
Node next;
}
private Node head;
private Node tail;
public void display()
{
if (head == null )
return ;
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
public void push( int data)
{
Node nn = new Node();
nn.data = data;
nn.next = null ;
if (head == null ) {
head = nn;
}
else {
Node temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = nn;
}
}
private void unfold(Node node)
{
if (node == null )
return ;
if (node.next == null ) {
head = tail = node;
return ;
}
else if (node.next.next == null ) {
head = node;
tail = node.next;
return ;
}
Node temp = node.next;
unfold(node.next.next);
node.next = head;
head = node;
tail.next = temp;
tail = temp;
tail.next = null ;
}
public static void main(String[] args)
{
GFG l = new GFG();
l.push( 1 );
l.push( 5 );
l.push( 2 );
l.push( 4 );
l.push( 3 );
l.display();
l.unfold(l.head);
l.display();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
head = None
tail = None
def display():
if (head = = None ):
return
temp = head
while (temp ! = None ):
print (temp.data, end = " " )
temp = temp. next
print ()
def push(data):
global head, tail
nn = Node(data)
if (head = = None ):
head = nn
else :
temp = head
while (temp. next ! = None ):
temp = temp. next
temp. next = nn
def unfold(node):
global head, tail
if (node = = None ):
return
if (node. next = = None ):
head = tail = node
return
elif (node. next . next = = None ):
head = node
tail = node. next
return
temp = node. next
unfold(node. next . next )
node. next = head
head = node
tail. next = temp
tail = temp
tail. next = None
if __name__ = = '__main__' :
push( 1 )
push( 5 )
push( 2 )
push( 4 )
push( 3 )
display()
unfold(head)
display()
|
C#
using System;
public class GFG {
private class Node {
public int data;
public Node next;
}
private Node head;
private Node tail;
public void display()
{
if (head == null )
return ;
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine();
}
public void push( int data)
{
Node nn = new Node();
nn.data = data;
nn.next = null ;
if (head == null ) {
head = nn;
}
else {
Node temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = nn;
}
}
private void unfold(Node node)
{
if (node == null )
return ;
if (node.next == null ) {
head = tail = node;
return ;
}
else if (node.next.next == null ) {
head = node;
tail = node.next;
return ;
}
Node temp = node.next;
unfold(node.next.next);
node.next = head;
head = node;
tail.next = temp;
tail = temp;
tail.next = null ;
}
public static void Main()
{
GFG l = new GFG();
l.push(1);
l.push(5);
l.push(2);
l.push(4);
l.push(3);
l.display();
l.unfold(l.head);
l.display();
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var head = null ;
var tail = null ;
function display()
{
if (head == null )
return ;
var temp = head;
while (temp != null ) {
document.write(temp.data + " " );
temp = temp.next;
}
document.write( "</br>" );
}
function push( data)
{
var nn = new Node();
nn.data = data;
nn.next = null ;
if (head == null ) {
head = nn;
}
else {
var temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = nn;
}
}
function unfold( node)
{
if (node == null )
return ;
if (node.next == null ) {
head = tail = node;
return ;
}
else if (node.next.next == null ) {
head = node;
tail = node.next;
return ;
}
var temp = node.next;
unfold(node.next.next);
node.next = head;
head = node;
tail.next = temp;
tail = temp;
tail.next = null ;
}
push(1);
push(5);
push(2);
push(4);
push(3);
display();
unfold(head);
display();
</script>
|
Output
1 5 2 4 3
1 2 3 4 5
Time Complexity: O(N), where N is the total number of nodes in the linked list.
Auxiliary Space: O(N)
Iterative Approach:-
Approach: First we have to segregate the linked list on the basis of even-odd index. Then we reverse the odd part of segregated list and joined with the first list. This approach does not use recursive space.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class ListNode {
public :
int val = 0;
ListNode* next = nullptr;
ListNode( int val) { this ->val = val; }
};
ListNode* reverse(ListNode* head)
{
ListNode *prev = NULL, *temp = head, *copy = NULL;
while (temp != NULL) {
copy = temp->next;
temp->next = prev;
prev = temp;
temp = copy;
}
return prev;
}
void unfold(ListNode* head)
{
int i = 0;
ListNode *prev1 = NULL, *prev2 = NULL, *h1 = head,
*h2 = head;
while (head != NULL) {
if (i % 2 == 0) {
if (prev1 == NULL) {
h1 = head;
prev1 = head;
}
else {
prev1->next = head;
prev1 = head;
}
}
else {
if (prev2 == NULL) {
h2 = head;
prev2 = head;
}
else {
prev2->next = head;
prev2 = head;
}
}
i++;
head = head->next;
}
prev2->next = NULL;
ListNode* rev
= reverse(h2);
prev1->next = rev;
}
void printList(ListNode* node)
{
ListNode* curr = node;
while (curr != nullptr) {
cout << curr->val << " " ;
curr = curr->next;
}
cout << endl;
}
int main()
{
int n;
ListNode* dummy = new ListNode(-1);
ListNode* prev = dummy;
n=5; int i=0;
int arr[]={1, 5, 2, 4, 3};
while (i < n) {
prev->next = new ListNode(arr[i]);
prev = prev->next;
i++;
}
ListNode* head = dummy->next;
printList(head);
unfold(head);
printList(head);
return 0;
}
|
Java
class GFG {
static class ListNode {
int val = 0 ;
ListNode next = null ;
ListNode( int val) { this .val = val; }
}
static ListNode reverse(ListNode head)
{
ListNode prev = null , temp = head, copy = null ;
while (temp != null ) {
copy = temp.next;
temp.next = prev;
prev = temp;
temp = copy;
}
return prev;
}
static void unfold(ListNode head)
{
int i = 0 ;
ListNode prev1 = null , prev2 = null , h1 = head,
h2 = head;
while (head != null ) {
if (i % 2 == 0 ) {
if (prev1 == null ) {
h1 = head;
prev1 = head;
}
else {
prev1.next = head;
prev1 = head;
}
}
else {
if (prev2 == null ) {
h2 = head;
prev2 = head;
}
else {
prev2.next = head;
prev2 = head;
}
}
i++;
head = head.next;
}
prev2.next = null ;
ListNode rev
= reverse(h2);
prev1.next
= rev;
}
static void printList(ListNode node)
{
ListNode curr = node;
while (curr != null ) {
System.out.print(curr.val + " " );
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args)
{
int n;
ListNode dummy = new ListNode(- 1 );
ListNode prev = dummy;
n = 5 ;
int i = 0 ;
int arr[] = { 1 , 5 , 2 , 4 ,
3 };
while (i < n) {
prev.next = new ListNode(arr[i]);
prev = prev.next;
i++;
}
ListNode head = dummy.next;
printList(head);
unfold(head);
printList(head);
}
}
|
Python3
class ListNode:
def __init__( self ,val):
self . next = None
self .val = val
def reverse(head):
prev,temp,copy = None ,head, None
while (temp ! = None ):
copy = temp. next
temp. next = prev
prev = temp
temp = copy
return prev
def unfold(head):
i = 0
prev1,prev2,h1,h2 = None , None ,head,head
while (head ! = None ):
if (i % 2 = = 0 ):
if (prev1 = = None ):
h1 = head
prev1 = head
else :
prev1. next = head
prev1 = head
else :
if (prev2 = = None ):
h2 = head
prev2 = head
else :
prev2. next = head
prev2 = head
i + = 1
head = head. next
prev2. next = None
rev = reverse(h2)
prev1. next = rev
def printList(node):
curr = node
while (curr ! = None ):
print (curr.val,end = " " )
curr = curr. next
print ()
dummy = ListNode( - 1 )
prev = dummy
n = 5
i = 0
arr = [ 1 , 5 , 2 , 4 , 3 ]
while (i < n):
prev. next = ListNode(arr[i])
prev = prev. next
i + = 1
head = dummy. next
printList(head)
unfold(head)
printList(head)
|
C#
using System;
class GFG {
class ListNode {
public int val = 0;
public ListNode next = null ;
public ListNode( int val) { this .val = val; }
}
static ListNode reverse(ListNode head)
{
ListNode prev = null , temp = head, copy = null ;
while (temp != null ) {
copy = temp.next;
temp.next = prev;
prev = temp;
temp = copy;
}
return prev;
}
static void unfold(ListNode head)
{
int i = 0;
ListNode prev1 = null , prev2 = null , h2 = head;
while (head != null ) {
if (i % 2 == 0) {
if (prev1 == null ) {
prev1 = head;
}
else {
prev1.next = head;
prev1 = head;
}
}
else {
if (prev2 == null ) {
h2 = head;
prev2 = head;
}
else {
prev2.next = head;
prev2 = head;
}
}
i++;
head = head.next;
}
prev2.next = null ;
ListNode rev = reverse(
h2);
prev1.next = rev;
}
static void printList(ListNode node)
{
ListNode curr = node;
while (curr != null ) {
Console.Write(curr.val + " " );
curr = curr.next;
}
Console.WriteLine();
}
static public void Main()
{
int n;
ListNode dummy = new ListNode(-1);
ListNode prev = dummy;
n = 5;
int i = 0;
int [] arr = { 1, 5, 2, 4,
3 };
while (i < n) {
prev.next = new ListNode(arr[i]);
prev = prev.next;
i++;
}
ListNode head = dummy.next;
printList(head);
unfold(head);
printList(head);
}
}
|
Javascript
<script>
class ListNode {
constructor(val){
this .next = null ;
this .val = val;
}
}
function reverse(head)
{
let prev = null , temp = head, copy = null ;
while (temp != null ) {
copy = temp.next;
temp.next = prev;
prev = temp;
temp = copy;
}
return prev;
}
function unfold(head)
{
let i = 0;
let prev1 = null , prev2 = null , h1 = head,h2 = head;
while (head != null ) {
if (i % 2 == 0) {
if (prev1 == null ) {
h1 = head;
prev1 = head;
}
else {
prev1.next = head;
prev1 = head;
}
}
else {
if (prev2 == null ) {
h2 = head;
prev2 = head;
}
else {
prev2.next = head;
prev2 = head;
}
}
i++;
head = head.next;
}
prev2.next = null ;
let rev = reverse(h2);
prev1.next = rev;
}
function printList(node)
{
let curr = node;
while (curr != null ) {
document.write(curr.val, " " );
curr = curr.next;
}
document.write( "</br>" );
}
let n;
let dummy = new ListNode(-1);
let prev = dummy;
n=5;
let i=0;
let arr = [1, 5, 2, 4, 3];
while (i < n) {
prev.next = new ListNode(arr[i]);
prev = prev.next;
i++;
}
let head = dummy.next;
printList(head);
unfold(head);
printList(head);
</script>
|
Output
1 5 2 4 3
1 2 3 4 5
Time Complexity: O(N), where N is the total number of nodes in the linked list.
Auxiliary Space: O(1)
Last Updated :
08 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...