Given a singly linked list L0 -> L1 -> … -> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 … You are required to do this in place without altering the nodes’ values.
Examples:
Input: 1 -> 2 -> 3 -> 4
Output: 1 -> 4 -> 2 -> 3
Input: 1 -> 2 -> 3 -> 4 -> 5
Output: 1 -> 5 -> 2 -> 4 -> 3
Simple Solution
1) Initialize current node as head.
2) While next of current node is not null, do following
a) Find the last node, remove it from the end and insert it as next
of the current node.
b) Move current to next of current
The time complexity of the above simple solution is O(n2) where n is the number of nodes in the linked list.
Better Solution:
- Copy contents of the given linked list to a vector.
- Rearrange the given vector by swapping nodes from both ends.
- Copy the modified vector back to the linked list.
Implementation of this approach: https://ide.geeksforgeeks.org/1eGSEy
Efficient Solution:
1) Find the middle point using tortoise and hare method.
2) Split the linked list into two halves using found middle point in step 1.
3) Reverse the second half.
4) Do alternate merge of first and second halves.
Below is the implementation of this method.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
void reverselist(Node** head)
{
Node *prev = NULL, *curr = *head, *next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
void printlist(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
if (head->next)
cout << "-> " ;
head = head->next;
}
cout << endl;
}
void rearrange(Node** head)
{
Node *slow = *head, *fast = slow->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
Node* head1 = *head;
Node* head2 = slow->next;
slow->next = NULL;
reverselist(&head2);
*head = newNode(0);
Node* curr = *head;
while (head1 || head2) {
if (head1) {
curr->next = head1;
curr = curr->next;
head1 = head1->next;
}
if (head2) {
curr->next = head2;
curr = curr->next;
head2 = head2->next;
}
}
*head = (*head)->next;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
printlist(head);
rearrange(&head);
printlist(head);
return 0;
}
|
Java
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void printlist(Node node)
{
if (node == null ) {
return ;
}
while (node != null ) {
System.out.print(node.data + " -> " );
node = node.next;
}
}
Node reverselist(Node node)
{
Node prev = null , curr = node, next;
while (curr != null ) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
node = prev;
return node;
}
void rearrange(Node node)
{
Node slow = node, fast = slow.next;
while (fast != null && fast.next != null ) {
slow = slow.next;
fast = fast.next.next;
}
Node node1 = node;
Node node2 = slow.next;
slow.next = null ;
node2 = reverselist(node2);
node = new Node( 0 );
Node curr = node;
while (node1 != null || node2 != null ) {
if (node1 != null ) {
curr.next = node1;
curr = curr.next;
node1 = node1.next;
}
if (node2 != null ) {
curr.next = node2;
curr = curr.next;
node2 = node2.next;
}
}
node = node.next;
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 1 );
list.head.next = new Node( 2 );
list.head.next.next = new Node( 3 );
list.head.next.next.next = new Node( 4 );
list.head.next.next.next.next = new Node( 5 );
list.printlist(head);
list.rearrange(head);
System.out.println( "" );
list.printlist(head);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
def printlist(node):
if (node = = None ):
return
while (node ! = None ):
print (node.data, " -> " , end = "")
node = node. next
def reverselist(node):
prev = None
curr = node
next = None
while (curr ! = None ):
next = curr. next
curr. next = prev
prev = curr
curr = next
node = prev
return node
def rearrange(node):
slow = node
fast = slow. next
while (fast ! = None and fast. next ! = None ):
slow = slow. next
fast = fast. next . next
node1 = node
node2 = slow. next
slow. next = None
node2 = reverselist(node2)
node = Node( 0 )
curr = node
while (node1 ! = None or node2 ! = None ):
if (node1 ! = None ):
curr. next = node1
curr = curr. next
node1 = node1. next
if (node2 ! = None ):
curr. next = node2
curr = curr. next
node2 = node2. next
node = node. next
head = None
head = Node( 1 )
head. next = Node( 2 )
head. next . next = Node( 3 )
head. next . next . next = Node( 4 )
head. next . next . next . next = Node( 5 )
printlist(head)
rearrange(head)
print ()
printlist(head)
|
C#
using System;
public class LinkedList {
Node head;
class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void printlist(Node node)
{
if (node == null ) {
return ;
}
while (node != null ) {
Console.Write(node.data + " -> " );
node = node.next;
}
}
Node reverselist(Node node)
{
Node prev = null , curr = node, next;
while (curr != null ) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
node = prev;
return node;
}
void rearrange(Node node)
{
Node slow = node, fast = slow.next;
while (fast != null && fast.next != null ) {
slow = slow.next;
fast = fast.next.next;
}
Node node1 = node;
Node node2 = slow.next;
slow.next = null ;
node2 = reverselist(node2);
node = new Node(0);
Node curr = node;
while (node1 != null || node2 != null ) {
if (node1 != null ) {
curr.next = node1;
curr = curr.next;
node1 = node1.next;
}
if (node2 != null ) {
curr.next = node2;
curr = curr.next;
node2 = node2.next;
}
}
node = node.next;
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.head.next.next.next = new Node(4);
list.head.next.next.next.next = new Node(5);
list.printlist(list.head);
list.rearrange(
list.head);
Console.WriteLine( "" );
list.printlist(list.head);
}
}
|
Javascript
<script>
var head;
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
}
function printlist(node) {
if (node == null ) {
return ;
}
while (node != null ) {
document.write(node.data + " -> " );
node = node.next;
}
}
function reverselist(node) {
var prev = null , curr = node, next;
while (curr != null ) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
node = prev;
return node;
}
function rearrange(node) {
var slow = node, fast = slow.next;
while (fast != null && fast.next != null ) {
slow = slow.next;
fast = fast.next.next;
}
var node1 = node;
var node2 = slow.next;
slow.next = null ;
node2 = reverselist(node2);
node = new Node(0);
var curr = node;
while (node1 != null || node2 != null ) {
if (node1 != null ) {
curr.next = node1;
curr = curr.next;
node1 = node1.next;
}
if (node2 != null ) {
curr.next = node2;
curr = curr.next;
node2 = node2.next;
}
}
node = node.next;
}
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
printlist(head);
rearrange(head);
document.write( "<br/>" );
printlist(head);
</script>
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* newNode( int key)
{
Node* temp = (Node*) malloc ( sizeof (Node));
temp->data = key;
temp->next = NULL;
return temp;
}
void reverselist(Node** head)
{
Node *prev = NULL, *curr = *head, *next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
void printlist(Node* head)
{
while (head != NULL) {
printf ( "%d " , head->data);
if (head->next)
printf ( "-> " );
head = head->next;
}
printf ( "\n" );
}
void rearrange(Node** head)
{
Node *slow = *head, *fast = slow->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
Node* head1 = *head;
Node* head2 = slow->next;
slow->next = NULL;
reverselist(&head2);
*head = newNode(0);
Node* curr = *head;
while (head1 || head2) {
if (head1) {
curr->next = head1;
curr = curr->next;
head1 = head1->next;
}
if (head2) {
curr->next = head2;
curr = curr->next;
head2 = head2->next;
}
}
*head = (*head)->next;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
printlist(head);
rearrange(&head);
printlist(head);
return 0;
}
|
Output1 -> 2 -> 3 -> 4 -> 5
1 -> 5 -> 2 -> 4 -> 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Another Approach: (Using recursion)
- Hold a pointer to the head node and go till the last node using recursion
- Once the last node is reached, start swapping the last node to the next of head node
- Move the head pointer to the next node
- Repeat this until the head and the last node meet or come adjacent to each other
- Once the Stop condition met, we need to discard the left nodes to fix the loop created in the list while swapping nodes.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
void printlist(Node* head)
{
while (head) {
cout << head->data;
if (head->next)
cout << "->" ;
head = head->next;
}
cout << endl;
}
void rearrange(Node** head, Node* last)
{
if (!last)
return ;
rearrange(head, last->next);
if (!(*head)->next)
return ;
if ((*head) != last && (*head)->next != last) {
Node* tmp = (*head)->next;
(*head)->next = last;
last->next = tmp;
*head = tmp;
}
else {
if ((*head) != last)
*head = (*head)->next;
(*head)->next = NULL;
}
}
int main()
{
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
printlist(head);
Node* tmp = head;
rearrange(&tmp, head);
printlist(head);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node next;
Node( int key)
{
data = key;
next = null ;
}
}
class GFG {
Node left = null ;
void printlist(Node head)
{
while (head != null ) {
System.out.print(head.data + " " );
if (head.next != null ) {
System.out.print( "->" );
}
head = head.next;
}
System.out.println();
}
void rearrange(Node head)
{
if (head != null ) {
left = head;
reorderListUtil(left);
}
}
void reorderListUtil(Node right)
{
if (right == null ) {
return ;
}
reorderListUtil(right.next);
if (left == null ) {
return ;
}
if (left != right && left.next != right) {
Node temp = left.next;
left.next = right;
right.next = temp;
left = temp;
}
else {
if (left.next == right) {
left.next.next = null ;
left = null ;
}
else {
left.next = null ;
left = null ;
}
}
}
public static void main(String[] args)
{
Node head = new Node( 1 );
head.next = new Node( 2 );
head.next.next = new Node( 3 );
head.next.next.next = new Node( 4 );
head.next.next.next.next = new Node( 5 );
GFG gfg = new GFG();
gfg.printlist(head);
gfg.rearrange(head);
gfg.printlist(head);
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self . next = None
left = None
def printlist(head):
while (head ! = None ):
print (head.data, end = " " )
if (head. next ! = None ):
print ( "->" , end = "")
head = head. next
print ()
def rearrange(head):
global left
if (head ! = None ):
left = head
reorderListUtil(left)
def reorderListUtil(right):
global left
if (right = = None ):
return
reorderListUtil(right. next )
if (left = = None ):
return
if (left ! = right and left. next ! = right):
temp = left. next
left. next = right
right. next = temp
left = temp
else :
if (left. next = = right):
left. next . next = None
left = None
else :
left. next = None
left = None
head = Node( 1 )
head. next = Node( 2 )
head. next . next = Node( 3 )
head. next . next . next = Node( 4 )
head. next . next . next . next = Node( 5 )
printlist(head)
rearrange(head)
printlist(head)
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int key)
{
data = key;
next = null ;
}
}
class GFG{
Node left = null ;
void printlist(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
if (head.next != null )
{
Console.Write( "->" );
}
head = head.next;
}
Console.WriteLine();
}
void rearrange(Node head)
{
if (head != null )
{
left = head;
reorderListUtil(left);
}
}
void reorderListUtil(Node right)
{
if (right == null )
{
return ;
}
reorderListUtil(right.next);
if (left == null )
{
return ;
}
if (left != right && left.next != right)
{
Node temp = left.next;
left.next = right;
right.next = temp;
left = temp;
}
else
{
if (left.next == right)
{
left.next.next = null ;
left = null ;
}
else
{
left.next = null ;
left = null ;
}
}
}
static public void Main()
{
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
GFG gfg = new GFG();
gfg.printlist(head);
gfg.rearrange(head);
gfg.printlist(head);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var left = null ;
function printlist(head) {
while (head != null ) {
document.write(head.data + " " );
if (head.next != null ) {
document.write( "->" );
}
head = head.next;
}
document.write( "<br/>" );
}
function rearrange(head) {
if (head != null ) {
left = head;
reorderListUtil(left);
}
}
function reorderListUtil(right) {
if (right == null ) {
return ;
}
reorderListUtil(right.next);
if (left == null ) {
return ;
}
if (left != right && left.next != right) {
var temp = left.next;
left.next = right;
right.next = temp;
left = temp;
} else {
if (left.next == right) {
left.next.next = null ;
left = null ;
} else {
left.next = null ;
left = null ;
}
}
}
var head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
printlist(head);
rearrange(head);
printlist(head);
</script>
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node;
Node* newNode( int key)
{
Node* temp = malloc ( sizeof (Node));
temp->data = key;
temp->next = NULL;
return temp;
}
void printlist(Node* head)
{
while (head) {
printf ( "%d " , head->data);
if (head->next)
printf ( "->" );
head = head->next;
}
printf ( "\n" );
}
void rearrange(Node** head, Node* last)
{
if (!last)
return ;
rearrange(head, last->next);
if (!(*head)->next)
return ;
if ((*head) != last && (*head)->next != last) {
Node* tmp = (*head)->next;
(*head)->next = last;
last->next = tmp;
*head = tmp;
}
else {
if ((*head) != last)
*head = (*head)->next;
(*head)->next = NULL;
}
}
int main()
{
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
printlist(head);
Node* tmp = head;
rearrange(&tmp, head);
printlist(head);
return 0;
}
|
Output1 ->2 ->3 ->4 ->5
1 ->5 ->2 ->4 ->3
Time complexity: O(N) where N is no of nodes of linked list
Auxiliary Space: O(1) since using constant space
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.