Split a Circular Linked List into two halves
Last Updated :
11 Sep, 2024
Given a Circular linked list. The task is split into two Circular Linked lists. If there are an odd number of nodes in the given circular linked list then out of the resulting two halved lists, the first list should have one node more than the second list.
Examples:
Input: 10->4->9
Output: 10->4 , 9
Explanation: Number of nodes in circular Linked List are odd, so it will split as shown below.
Input: head: 10->4->9->7
Output: 10->4 , 9->7
Explanation: Number of nodes in circular Linked List are even, so it will split from two equal halves.
Approach:
The idea is to find the middle and last nodes using Hare and Tortoise Algorithm. Traverse linked list using a slow pointer and a fast pointer. Move the slow pointer to the next node(one node forward) and the fast pointer to the next of the next node(two nodes forward). When the fast pointer reaches the last node or second last node, then the slow pointer will reach the middle of the linked list. We can now easily split circular linked list and then point the tail nodes to their respective head nodes.
Step-by-step approach:
- Traverse the circular linked list using the fast and slow pointer technique to find the middle and last nodes. The slow pointer will reach the middle, and the fast pointer will reach the end.
- If the list has an odd number of nodes, the fast pointer will reach the last node. If the list has an even number, it will stop just before the last node.
- Once the middle node is found, split the list into two halves. The first half starts from the head, and the second half starts from the node after the middle node.
- Update the next pointers of the middle and last nodes to make each half circular by pointing them to their respective head nodes.
- Function returns two circular linked lists representing the split halves.
Below is the implementation of above approach:
C++
// C++ Program to split a circular linked list
// into two halves
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node (int new_value){
data = new_value;
next = nullptr;
}
};
// Function to split a list (starting with head)
// into two lists.
pair<Node*, Node*> splitList(Node *head) {
Node *slow = head;
Node *fast = head;
if(head == nullptr)
return {nullptr, nullptr};
// For odd nodes, fast->next is head and
// for even nodes, fast->next->next is head
while(fast->next != head &&
fast->next->next != head) {
fast = fast->next->next;
slow = slow->next;
}
// If there are even elements in list
// then move fast
if(fast->next->next == head)
fast = fast->next;
// Set the head pointer of first half
Node* head1 = head;
// Set the head pointer of second half
Node* head2 = slow->next;
// Make second half circular
fast->next = slow->next;
// Make first half circular
slow->next = head;
return {head1, head2};
}
void printList(Node *head) {
Node *curr = head;
if(head != nullptr) {
do {
cout << curr->data << " ";
curr = curr->next;
} while(curr != head);
cout << endl;
}
}
int main() {
Node *head = new Node(1);
Node *head1 = nullptr;
Node *head2 = nullptr;
// Created linked list will be 1->2->3->4
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = head;
pair<Node*, Node*> result = splitList(head);
head1 = result.first;
head2 = result.second;
printList(head1);
printList(head2);
return 0;
}
C
// C Program to split a circular linked list
// into two halves
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Pair {
struct Node* first;
struct Node* second;
};
// Function to split a list (starting with head)
// into two lists and return a pair of their heads
struct Pair splitList(struct Node* head) {
struct Node* slow = head;
struct Node* fast = head;
struct Pair result;
if (head == NULL) {
result.first = NULL;
result.second = NULL;
return result;
}
// For odd nodes, fast->next is head and
// for even nodes, fast->next->next is head
while (fast->next != head
&& fast->next->next != head) {
fast = fast->next->next;
slow = slow->next;
}
// If there are even elements in the list
// then move fast
if (fast->next->next == head) {
fast = fast->next;
}
// Set the head pointer of the first half
result.first = head;
// Set the head pointer of the second half
result.second = slow->next;
// Make the second half circular
fast->next = slow->next;
// Make the first half circular
slow->next = head;
return result;
}
void printList(struct Node* head) {
struct Node* curr = head;
if (head != NULL) {
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != head);
printf("\n");
}
}
struct Node* createNode(int new_data) {
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
struct Node* head1 = NULL;
struct Node* head2 = NULL;
// Created linked list will be 1->2->3->4
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = head;
struct Pair result = splitList(head);
head1 = result.first;
head2 = result.second;
printList(head1);
printList(head2);
return 0;
}
Java
// Java Program to split a circular
// linked list into two halves
class Node {
int data;
Node next;
Node(int newValue) {
data = newValue;
next = null;
}
}
class Pair {
Node first;
Node second;
Pair(Node first, Node second) {
this.first = first;
this.second = second;
}
}
public class GfG {
// Function to split a list into two lists.
static Pair splitList(Node head) {
Node slow = head;
Node fast = head;
if (head == null) {
return new Pair(null, null);
}
// For odd nodes, fast.next is head and
// for even nodes, fast.next.next is head
while (fast.next != head &&
fast.next.next != head) {
fast = fast.next.next;
slow = slow.next;
}
// If there are even elements in
// the list then move fast
if (fast.next.next == head) {
fast = fast.next;
}
// Set the head pointer of the first half
Node head1 = head;
// Set the head pointer of the second half
Node head2 = slow.next;
// Make the second half circular
fast.next = slow.next;
// Make the first half circular
slow.next = head;
return new Pair(head1, head2);
}
static void printList(Node head) {
Node curr = head;
if (head != null) {
do {
System.out.print(curr.data + " ");
curr = curr.next;
} while (curr != head);
System.out.println();
}
}
public static void main(String[] args) {
Node head1 = null;
Node head2 = null;
// Created linked list will be 1->2->3->4
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 = head;
Pair result = splitList(head);
head1 = result.first;
head2 = result.second;
printList(head1);
printList(head2);
}
}
Python
# Python Program to split a circular linked list
# into two halves
class Node:
def __init__(self, new_value):
self.data = new_value
self.next = None
# Function to split a list (starting with head)
# into two lists.
def split_list(head):
slow = head
fast = head
if head is None:
return None, None
# For odd nodes, fast->next is head and
# for even nodes, fast->next->next is head
while fast.next != head and fast.next.next != head:
fast = fast.next.next
slow = slow.next
# If there are even elements in list
# then move fast
if fast.next.next == head:
fast = fast.next
# Set the head pointer of first half
head1 = head
# Set the head pointer of second half
head2 = slow.next
# Make second half circular
fast.next = slow.next
# Make first half circular
slow.next = head
return head1, head2
def print_list(head):
curr = head
if head is not None:
while True:
print(curr.data , end=" ")
curr = curr.next
if curr == head:
break
print()
if __name__ == "__main__":
head1 = None
head2 = None
# Created linked list will be 1->2->3->4
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = head
head1, head2 = split_list(head)
print_list(head1)
print_list(head2)
C#
// C# Program to split a circular linked list
// into two halves
using System;
class Node {
public int Data;
public Node next;
public Node(int newValue) {
Data = newValue;
next = null;
}
}
// Function to split a list (starting with head)
// into two lists.
class GfG {
static (Node, Node) SplitList(Node head) {
Node slow = head;
Node fast = head;
if (head == null)
return (null, null);
// For odd nodes, fast->next is head and
// for even nodes, fast->next->next is head
while (fast.next != head && fast.next.next != head) {
fast = fast.next.next;
slow = slow.next;
}
// If there are even elements in list
// then move fast
if (fast.next.next == head)
fast = fast.next;
// Set the head pointer of first half
Node head1 = head;
// Set the head pointer of second half
Node head2 = slow.next;
// Make second half circular
fast.next = slow.next;
// Make first half circular
slow.next = head;
return (head1, head2);
}
static void PrintList(Node head) {
Node curr = head;
if (head != null) {
do {
Console.Write(" " + curr.Data);
curr = curr.next;
} while (curr != head);
Console.WriteLine();
}
}
static void Main() {
Node head1 = null;
Node head2 = null;
// Created linked list will be 1->2->3->4
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 = head;
var result = SplitList(head);
head1 = result.Item1;
head2 = result.Item2;
PrintList(head1);
PrintList(head2);
}
}
JavaScript
// JavaScript Program to split a circular linked list
// into two halves
class Node {
constructor(newValue) {
this.data = newValue;
this.next = null;
}
}
// Function to split a list
// into two lists.
function splitList(head) {
let slow = head;
let fast = head;
if (head === null)
return [null, null];
// For odd nodes, fast->next is head and
// for even nodes, fast->next->next is head
while (fast.next !== head &&
fast.next.next !== head) {
fast = fast.next.next;
slow = slow.next;
}
// If there are even elements in list
// then move fast
if (fast.next.next === head)
fast = fast.next;
// Set the head pointer of first half
const head1 = head;
// Set the head pointer of second half
const head2 = slow.next;
// Make second half circular
fast.next = slow.next;
// Make first half circular
slow.next = head;
return [head1, head2];
}
function printList(head) {
let curr = head;
if (head !== null) {
do {
console.log(curr.data);
curr = curr.next;
} while (curr !== head);
console.log()
}
}
// Create a circular linked list: 1->2->3->4
let 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 = head;
const [head1, head2] = splitList(head);
printList(head1);
printList(head2);
Time Complexity: O(n), where n is the number of nodes in the circular Linked List.
Auxiliary Space: O(1)
Similar Reads
Split a Circular Linked List into three halves of almost same size Split given Circular Linked List into three halves without calculating its length such that the difference between a linked list with a maximum number of nodes and a linked list with a minimum number of nodes is minimum. Examples: Input: Circular Linked List: 1->3->5->7->9Output: 1 3 5 7
10 min read
Circular Linked List in Python A Circular Linked List is a variation of the standard linked list. In a standard linked list, the last element points to null, indicating the end of the list. However, in a circular linked list, the last element points back to the first element, forming a loop. In this article, we will discuss the c
13 min read
Insertion in an empty List in the circular linked list A circular linked list is a type of data structure where each node points to the next one, and the last node connects back to the first, forming a circle. This setup allows you to loop through the list without stopping. Knowing how to insert a node into an empty circular linked list is important in
5 min read
Insertion at the end in circular linked list A circular linked list is a data structure where each node points to the next, and the last node connects back to the first, creating a loop. Insertion at the end in circular linked list is an important operation. Understanding how to perform this insertion is essential for effectively manage and us
7 min read
Introduction to Circular Linked List A circular linked list is a data structure where the last node connects back to the first, forming a loop. This structure allows for continuous traversal without any interruptions. Circular linked lists are especially helpful for tasks like scheduling and managing playlists, allowing for smooth navi
15+ min read