Rearrange a linked list such that all even and odd positioned nodes are together

• Difficulty Level : Medium
• Last Updated : 02 Nov, 2021

Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together,
Examples:

Input:   1->2->3->4
Output:  1->3->2->4

Input:   10->22->30->43->56->70
Output:  10->30->56->22->43->70

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The important thing in this question is to make sure that all below cases are handled
2) A linked list with only one node
3) A linked list with only two nodes
4) A linked list with an odd number of nodes
5) A linked list with an even number of nodes
The below program maintains two pointers ‘odd’ and ‘even’ for current nodes at odd and even positions respectively. We also store the first node of even linked list so that we can attach the even list at the end of odd list after all odd and even nodes are connected together in two different lists.

C++

// C++ program to rearrange a linked list in such a
// way that all odd positioned node are stored before
// all even positioned nodes
#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
};

// A utility function to create a new node
Node* newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}

// Rearranges given linked list such that all even
// positioned nodes are before odd positioned.
{
// Corner case
return NULL;

// Initialize first nodes of even and
// odd lists

// Remember the first node of even list so
// that we can connect the even list at the
// end of odd list.
Node *evenFirst = even;

while (1)
{
// If there are no more nodes, then connect
// first node of even list to the last node
// of odd list
if (!odd || !even || !(even->next))
{
odd->next = evenFirst;
break;
}

// Connecting odd nodes
odd->next = even->next;
odd = even->next;

// If there are NO more even nodes after
// current odd.
if (odd->next == NULL)
{
even->next = NULL;
odd->next = evenFirst;
break;
}

// Connecting even nodes
even->next = odd->next;
even = odd->next;
}

}

// A utility function to print a linked list
void printlist(Node * node)
{
while (node != NULL)
{
cout << node->data << "->";
node = node->next;
}
cout << "NULL" << endl;
}

// Driver code
int main(void)
{

return 0;
}

// This is code is contributed by rathbhupendra

C

// C program to rearrange a linked list in such a
// way that all odd positioned node are stored before
// all even positioned nodes
#include<bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node* next;
};

// A utility function to create a new node
Node* newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}

// Rearranges given linked list such that all even
// positioned nodes are before odd positioned.
{
// Corner case
return NULL;

// Initialize first nodes of even and
// odd lists

// Remember the first node of even list so
// that we can connect the even list at the
// end of odd list.
Node *evenFirst = even;

while (1)
{
// If there are no more nodes, then connect
// first node of even list to the last node
// of odd list
if (!odd || !even || !(even->next))
{
odd->next = evenFirst;
break;
}

// Connecting odd nodes
odd->next = even->next;
odd = even->next;

// If there are NO more even nodes after
// current odd.
if (odd->next == NULL)
{
even->next = NULL;
odd->next = evenFirst;
break;
}

// Connecting even nodes
even->next = odd->next;
even = odd->next;
}

}

// A utility function to print a linked list
void printlist(Node * node)
{
while (node != NULL)
{
cout << node->data << "->";
node = node->next;
}
cout << "NULL" << endl;
}

// Driver code
int main(void)
{

return 0;
}

Java

// Java program to rearrange a linked list
// in such a way that all odd positioned
// node are stored before all even positioned nodes
class GfG
{

static class Node
{
int data;
Node next;
}

// A utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.data = key;
temp.next = null;
return temp;
}

// such that all even positioned
// nodes are before odd positioned.
{
// Corner case
return null;

// Initialize first nodes of even and
// odd lists

// Remember the first node of even list so
// that we can connect the even list at the
// end of odd list.
Node evenFirst = even;

while (1 == 1)
{
// If there are no more nodes,
// then connect first node of even
// list to the last node of odd list
if (odd == null || even == null ||
(even.next) == null)
{
odd.next = evenFirst;
break;
}

// Connecting odd nodes
odd.next = even.next;
odd = even.next;

// If there are NO more even nodes
// after current odd.
if (odd.next == null)
{
even.next = null;
odd.next = evenFirst;
break;
}

// Connecting even nodes
even.next = odd.next;
even = odd.next;
}
}

// A utility function to print a linked list
static void printlist(Node node)
{
while (node != null)
{
System.out.print(node.data + "->");
node = node.next;
}
System.out.println("NULL") ;
}

// Driver code
public static void main(String[] args)
{

}
}

// This code is contributed by Prerna saini

Python3

# Python3 program to rearrange a linked list
# in such a way that all odd positioned
# node are stored before all even positioned nodes

class Node:
def __init__(self, d):
self.data = d
self.next = None

def __init__(self):

# A utility function to create
# a new node
def newNode(self, key):
temp = Node(key)
self.next = None
return temp

# such that all even positioned
# nodes are before odd positioned.

# Corner case
return None

# Initialize first nodes of
# even and odd lists

# Remember the first node of even list so
# that we can connect the even list at the
# end of odd list.
evenFirst = even

while (1 == 1):

# If there are no more nodes,
# then connect first node of even
# list to the last node of odd list
if (odd == None or even == None or
(even.next) == None):
odd.next = evenFirst
break

# Connecting odd nodes
odd.next = even.next
odd = even.next

# If there are NO more even nodes
# after current odd.
if (odd.next == None):
even.next = None
odd.next = evenFirst
break

# Connecting even nodes
even.next = odd.next
even = odd.next

# A utility function to print a linked list
def printlist(self, node):
while (node != None):
print(node.data, end = "")
print("->", end = "")
node = node.next
print ("NULL")

# Function to insert a new node
# at the beginning
def push(self, new_data):
new_node = Node(new_data)

# Driver code
ll.push(5)
ll.push(4)
ll.push(3)
ll.push(2)
ll.push(1)

ll.printlist(start)

# This code is contributed by Prerna Saini

C#

// C# program to rearrange a linked list
// in such a way that all odd positioned
// node are stored before all even positioned nodes
using System;

class GfG
{

class Node
{
public int data;
public Node next;
}

// A utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.data = key;
temp.next = null;
return temp;
}

// such that all even positioned
// nodes are before odd positioned.
{
// Corner case
return null;

// Initialize first nodes of even and
// odd lists

// Remember the first node of even list so
// that we can connect the even list at the
// end of odd list.
Node evenFirst = even;

while (1 == 1)
{
// If there are no more nodes,
// then connect first node of even
// list to the last node of odd list
if (odd == null || even == null ||
(even.next) == null)
{
odd.next = evenFirst;
break;
}

// Connecting odd nodes
odd.next = even.next;
odd = even.next;

// If there are NO more even nodes
// after current odd.
if (odd.next == null)
{
even.next = null;
odd.next = evenFirst;
break;
}

// Connecting even nodes
even.next = odd.next;
even = odd.next;
}
}

// A utility function to print a linked list
static void printlist(Node node)
{
while (node != null)
{
Console.Write(node.data + "->");
node = node.next;
}
Console.WriteLine("NULL") ;
}

// Driver code
public static void Main()
{

}
}

/* This code is contributed PrinciRaj1992 */

Javascript

<script>

// Javascript program to rearrange a linked list
// in such a way that all odd positioned
// node are stored before all even positioned nodes

class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
// A utility function to create a new node
function newNode(key) {
var temp = new Node();
temp.data = key;
temp.next = null;
return temp;
}

// such that all even positioned
// nodes are before odd positioned.
// Corner case
return null;

// Initialize first nodes of even and
// odd lists

// Remember the first node of even list so
// that we can connect the even list at the
// end of odd list.
var evenFirst = even;

while (1 == 1) {
// If there are no more nodes,
// then connect first node of even
// list to the last node of odd list
if (odd == null || even == null ||
(even.next) == null)
{
odd.next = evenFirst;
break;
}

// Connecting odd nodes
odd.next = even.next;
odd = even.next;

// If there are NO more even nodes
// after current odd.
if (odd.next == null) {
even.next = null;
odd.next = evenFirst;
break;
}

// Connecting even nodes
even.next = odd.next;
even = odd.next;
}
}

// A utility function to print a linked list
function printlist(node) {
while (node != null) {
document.write(node.data + "->");
node = node.next;
}
document.write("NULL<br/>");
}

// Driver code

// This code contributed by gauravrajput1

</script>

Output:

1->2->3->4->5->NULL