Rearrange a linked list such that all even and odd positioned nodes are together
Last Updated :
25 Apr, 2025
Given a linked list, the task is to rearrange the list in such a way that all odd positioned nodes are together and all even positioned nodes 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
Input: 1 -> 2
Output: 1 -> 2
Using 4 Pointers - O(n) time and O(1) Space
The idea is to separate nodes based on their positions by creating two separate sublists - one for odd positions and one for even positions - then connect the end of the odd list to the start of the even list, effectively rearranging the original list so all odd-positioned nodes appear before all even-positioned nodes.
Step by step approach:
- Check if the list is empty or has only one node, in which case no rearrangement is needed
- Traverse the list once, maintaining separate odd and even sub lists using position counters
- Add each node to either the odd or even sub list based on its position
- Connect the end of the odd list to the start of the even list
- Set the last node of the even list to point to null, marking the end of the rearranged list
C++
// C++ program to Rearrange a linked list such that
// all even and odd positioned nodes are together
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node (int x) {
data = x;
next = nullptr;
}
};
void rearrangeEvenOdd(Node *head) {
if (head == nullptr || head->next == nullptr) {
return;
}
Node* curr = head;
Node* oddStart = nullptr;
Node* oddEnd = nullptr;
Node* evenStart = nullptr;
Node* evenEnd = nullptr;
int i = 1;
while (curr != nullptr) {
// Even position
if (i % 2 == 0) {
if (evenStart == nullptr) {
evenStart = curr;
}
else {
evenEnd->next = curr;
}
evenEnd = curr;
}
// Odd position
else {
if (oddStart == nullptr) {
oddStart = curr;
}
else {
oddEnd->next = curr;
}
oddEnd = curr;
}
curr = curr->next;
i++;
}
// Join odd end with even start
oddEnd->next = evenStart;
// Mark the end of the linked list
if (evenEnd != nullptr) {
evenEnd->next = nullptr;
}
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
rearrangeEvenOdd(head);
printList(head);
return 0;
}
Java
// Java program to Rearrange a linked list such that
// all even and odd positioned nodes are together
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Rearranges the list so that even and
// odd positioned nodes are together
static void rearrangeEvenOdd(Node head) {
if (head == null || head.next == null) {
return;
}
Node curr = head;
Node oddStart = null;
Node oddEnd = null;
Node evenStart = null;
Node evenEnd = null;
int i = 1;
while (curr != null) {
// Even position
if (i % 2 == 0) {
if (evenStart == null) {
evenStart = curr;
}
else {
evenEnd.next = curr;
}
evenEnd = curr;
}
// Odd position
else {
if (oddStart == null) {
oddStart = curr;
}
else {
oddEnd.next = curr;
}
oddEnd = curr;
}
curr = curr.next;
i++;
}
// Join odd end with even start
oddEnd.next = evenStart;
// Mark the end of the linked list
if (evenEnd != null) {
evenEnd.next = null;
}
}
// Prints the linked list
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
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);
rearrangeEvenOdd(head);
printList(head);
}
}
Python
# Python program to Rearrange a linked list such that
# all even and odd positioned nodes are together
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Rearranges the list so that even and
# odd positioned nodes are together
def rearrangeEvenOdd(head):
if head is None or head.next is None:
return
curr = head
oddStart = None
oddEnd = None
evenStart = None
evenEnd = None
i = 1
while curr is not None:
# Even position
if i % 2 == 0:
if evenStart is None:
evenStart = curr
else:
evenEnd.next = curr
evenEnd = curr
# Odd position
else:
if oddStart is None:
oddStart = curr
else:
oddEnd.next = curr
oddEnd = curr
curr = curr.next
i += 1
# Join odd end with even start
oddEnd.next = evenStart
# Mark the end of the linked list
if evenEnd is not None:
evenEnd.next = None
# Prints the linked list
def printList(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
rearrangeEvenOdd(head)
printList(head)
C#
// C# program to Rearrange a linked list such that
// all even and odd positioned nodes are together
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Rearranges the list so that even
// and odd positioned nodes are together
static void rearrangeEvenOdd(Node head) {
if (head == null || head.next == null) {
return;
}
Node curr = head;
Node oddStart = null;
Node oddEnd = null;
Node evenStart = null;
Node evenEnd = null;
int i = 1;
while (curr != null) {
// Even position
if (i % 2 == 0) {
if (evenStart == null) {
evenStart = curr;
}
else {
evenEnd.next = curr;
}
evenEnd = curr;
}
// Odd position
else {
if (oddStart == null) {
oddStart = curr;
}
else {
oddEnd.next = curr;
}
oddEnd = curr;
}
curr = curr.next;
i++;
}
// Join odd end with even start
oddEnd.next = evenStart;
// Mark the end of the linked list
if (evenEnd != null) {
evenEnd.next = null;
}
}
// Prints the linked list
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
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);
rearrangeEvenOdd(head);
printList(head);
}
}
JavaScript
// JavaScript program to Rearrange a linked list such that
// all even and odd positioned nodes are together
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Rearranges the list so that even and odd positioned nodes are together
function rearrangeEvenOdd(head) {
if (head == null || head.next == null) {
return;
}
let curr = head;
let oddStart = null;
let oddEnd = null;
let evenStart = null;
let evenEnd = null;
let i = 1;
while (curr != null) {
// Even position
if (i % 2 === 0) {
if (evenStart == null) {
evenStart = curr;
}
else {
evenEnd.next = curr;
}
evenEnd = curr;
}
// Odd position
else {
if (oddStart == null) {
oddStart = curr;
}
else {
oddEnd.next = curr;
}
oddEnd = curr;
}
curr = curr.next;
i++;
}
// Join odd end with even start
oddEnd.next = evenStart;
// Mark the end of the linked list
if (evenEnd != null) {
evenEnd.next = null;
}
}
// Prints the linked list
function printList(head) {
let curr = head;
while (curr != null) {
process.stdout.write(curr.data + " ");
curr = curr.next;
}
console.log();
}
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
rearrangeEvenOdd(head);
printList(head);
Using Dummy Odd Even Nodes - O(n) time and O(1) space
The core logic of this approach is same as the above. Here we use dummy head nodes for two separate lists (odd and even positions), which simplifies the list manipulation process by eliminating special cases for the first nodes, then connecting these lists together after the original list is fully traversed.
C++
// C++ program to Rearrange a linked list such that
// all even and odd positioned nodes are together
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node (int x) {
data = x;
next = nullptr;
}
};
void rearrangeEvenOdd(Node *head) {
if (head == nullptr || head->next == nullptr) {
return;
}
// Create dummy heads for odd and even lists
Node* oddHead = new Node(0);
Node* evenHead = new Node(0);
// Working pointers for odd and even lists
Node* odd = oddHead;
Node* even = evenHead;
// Pointer to traverse original list
Node* curr = head;
int position = 1;
// Traverse the original linked list
while (curr != nullptr) {
if (position % 2 == 1) {
// Odd position node
odd->next = curr;
odd = odd->next;
}
else {
// Even position node
even->next = curr;
even = even->next;
}
curr = curr->next;
position++;
}
// Connect odd list with even list
odd->next = evenHead->next;
// Mark the end of the resultant list
even->next = nullptr;
// Update head to the first odd node
head = oddHead->next;
// Delete dummy nodes to prevent memory leak
delete oddHead;
delete evenHead;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
rearrangeEvenOdd(head);
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
return 0;
}
Java
// Java program to Rearrange a linked list such that
// all even and odd positioned nodes are together
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
static void rearrangeEvenOdd(Node head) {
if (head == null || head.next == null) {
return;
}
// Create dummy heads for odd and even lists
Node oddHead = new Node(0);
Node evenHead = new Node(0);
// Working pointers for odd and even lists
Node odd = oddHead;
Node even = evenHead;
// Pointer to traverse original list
Node curr = head;
int position = 1;
// Traverse the original linked list
while (curr != null) {
if (position % 2 == 1) {
// Odd position node
odd.next = curr;
odd = odd.next;
} else {
// Even position node
even.next = curr;
even = even.next;
}
curr = curr.next;
position++;
}
// Connect odd list with even list
odd.next = evenHead.next;
// Mark the end of the resultant list
even.next = null;
// Update head to the first odd node
head = oddHead.next;
}
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);
rearrangeEvenOdd(head);
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
}
Python
# Python program to Rearrange a linked list such that
# all even and odd positioned nodes are together
class Node:
def __init__(self, x):
self.data = x
self.next = None
def rearrangeEvenOdd(head):
if head is None or head.next is None:
return
# Create dummy heads for odd and even lists
oddHead = Node(0)
evenHead = Node(0)
# Working pointers for odd and even lists
odd = oddHead
even = evenHead
# Pointer to traverse original list
curr = head
position = 1
# Traverse the original linked list
while curr is not None:
if position % 2 == 1:
# Odd position node
odd.next = curr
odd = odd.next
else:
# Even position node
even.next = curr
even = even.next
curr = curr.next
position += 1
# Connect odd list with even list
odd.next = evenHead.next
# Mark the end of the resultant list
even.next = None
# Update head to the first odd node
head = oddHead.next
if __name__ == "__main__":
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
rearrangeEvenOdd(head)
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
C#
// C# program to Rearrange a linked list such that
// all even and odd positioned nodes are together
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
static void rearrangeEvenOdd(Node head) {
if (head == null || head.next == null) {
return;
}
// Create dummy heads for odd and even lists
Node oddHead = new Node(0);
Node evenHead = new Node(0);
// Working pointers for odd and even lists
Node odd = oddHead;
Node even = evenHead;
// Pointer to traverse original list
Node curr = head;
int position = 1;
// Traverse the original linked list
while (curr != null) {
if (position % 2 == 1) {
// Odd position node
odd.next = curr;
odd = odd.next;
} else {
// Even position node
even.next = curr;
even = even.next;
}
curr = curr.next;
position++;
}
// Connect odd list with even list
odd.next = evenHead.next;
// Mark the end of the resultant list
even.next = null;
// Update head to the first odd node
head = oddHead.next;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.Console.Write(curr.data + " ");
curr = curr.next;
}
System.Console.WriteLine();
}
static 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);
rearrangeEvenOdd(head);
printList(head);
}
}
JavaScript
// JavaScript program to Rearrange a linked list such that
// all even and odd positioned nodes are together
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
function rearrangeEvenOdd(head) {
if (head === null || head.next === null) {
return;
}
// Create dummy heads for odd and even lists
let oddHead = new Node(0);
let evenHead = new Node(0);
// Working pointers for odd and even lists
let odd = oddHead;
let even = evenHead;
// Pointer to traverse original list
let curr = head;
let position = 1;
// Traverse the original linked list
while (curr !== null) {
if (position % 2 === 1) {
// Odd position node
odd.next = curr;
odd = odd.next;
} else {
// Even position node
even.next = curr;
even = even.next;
}
curr = curr.next;
position++;
}
// Connect odd list with even list
odd.next = evenHead.next;
// Mark the end of the resultant list
even.next = null;
// Update head to the first odd node
head = oddHead.next;
}
function printList(head) {
let curr = head;
while (curr !== null) {
process.stdout.write(curr.data + " ");
curr = curr.next;
}
console.log();
}
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
rearrangeEvenOdd(head);
printList(head);
Rearrange a linked list such that all even and odd positioned nodes are together