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] Split Circular Linked List – O(n) Time and O(1) Space
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.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intnew_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 headwhile(fast->next!=head&&fast->next->next!=head){fast=fast->next->next;slow=slow->next;}// If there are even elements in list// then move fastif(fast->next->next==head)fast=fast->next;// Set the head pointer of first halfNode*head1=head;// Set the head pointer of second halfNode*head2=slow->next;// Make second half circularfast->next=slow->next;// Make first half circularslow->next=head;return{head1,head2};}voidprintList(Node*head){Node*curr=head;if(head!=nullptr){do{cout<<curr->data<<" ";curr=curr->next;}while(curr!=head);cout<<endl;}}intmain(){Node*head=newNode(1);Node*head1=nullptr;Node*head2=nullptr;// Created linked list will be 1->2->3->4head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=head;pair<Node*,Node*>result=splitList(head);head1=result.first;head2=result.second;printList(head1);printList(head2);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};structPair{structNode*first;structNode*second;};// Function to split a list (starting with head)// into two lists and return a pair of their headsstructPairsplitList(structNode*head){structNode*slow=head;structNode*fast=head;structPairresult;if(head==NULL){result.first=NULL;result.second=NULL;returnresult;}// For odd nodes, fast->next is head and// for even nodes, fast->next->next is headwhile(fast->next!=head&&fast->next->next!=head){fast=fast->next->next;slow=slow->next;}// If there are even elements in the list// then move fastif(fast->next->next==head){fast=fast->next;}// Set the head pointer of the first halfresult.first=head;// Set the head pointer of the second halfresult.second=slow->next;// Make the second half circularfast->next=slow->next;// Make the first half circularslow->next=head;returnresult;}voidprintList(structNode*head){structNode*curr=head;if(head!=NULL){do{printf("%d ",curr->data);curr=curr->next;}while(curr!=head);printf("\n");}}structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}intmain(){structNode*head1=NULL;structNode*head2=NULL;// Created linked list will be 1->2->3->4structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(3);head->next->next->next=createNode(4);head->next->next->next->next=head;structPairresult=splitList(head);head1=result.first;head2=result.second;printList(head1);printList(head2);return0;}
Java
classNode{intdata;Nodenext;Node(intnewValue){data=newValue;next=null;}}classPair{Nodefirst;Nodesecond;Pair(Nodefirst,Nodesecond){this.first=first;this.second=second;}}publicclassGfG{// Function to split a list into two lists.staticPairsplitList(Nodehead){Nodeslow=head;Nodefast=head;if(head==null){returnnewPair(null,null);}// For odd nodes, fast.next is head and// for even nodes, fast.next.next is headwhile(fast.next!=head&&fast.next.next!=head){fast=fast.next.next;slow=slow.next;}// If there are even elements in // the list then move fastif(fast.next.next==head){fast=fast.next;}// Set the head pointer of the first halfNodehead1=head;// Set the head pointer of the second halfNodehead2=slow.next;// Make the second half circularfast.next=slow.next;// Make the first half circularslow.next=head;returnnewPair(head1,head2);}staticvoidprintList(Nodehead){Nodecurr=head;if(head!=null){do{System.out.print(curr.data+" ");curr=curr.next;}while(curr!=head);System.out.println();}}publicstaticvoidmain(String[]args){Nodehead1=null;Nodehead2=null;// Created linked list will be 1->2->3->4Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=head;Pairresult=splitList(head);head1=result.first;head2=result.second;printList(head1);printList(head2);}}
Python
classNode:def__init__(self,new_value):self.data=new_valueself.next=None# Function to split a list (starting with head) # into two lists.defsplit_list(head):slow=headfast=headifheadisNone:returnNone,None# For odd nodes, fast->next is head and # for even nodes, fast->next->next is headwhilefast.next!=headandfast.next.next!=head:fast=fast.next.nextslow=slow.next# If there are even elements in list# then move fastiffast.next.next==head:fast=fast.next# Set the head pointer of first halfhead1=head# Set the head pointer of second halfhead2=slow.next# Make second half circularfast.next=slow.next# Make first half circularslow.next=headreturnhead1,head2defprint_list(head):curr=headifheadisnotNone:whileTrue:print(curr.data,end=" ")curr=curr.nextifcurr==head:breakprint()if__name__=="__main__":head1=Nonehead2=None# Created linked list will be 1->2->3->4head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=headhead1,head2=split_list(head)print_list(head1)print_list(head2)
C#
usingSystem;classNode{publicintData;publicNodenext;publicNode(intnewValue){Data=newValue;next=null;}}// Function to split a list (starting with head) // into two lists.classGfG{static(Node,Node)SplitList(Nodehead){Nodeslow=head;Nodefast=head;if(head==null)return(null,null);// For odd nodes, fast->next is head and // for even nodes, fast->next->next is headwhile(fast.next!=head&&fast.next.next!=head){fast=fast.next.next;slow=slow.next;}// If there are even elements in list// then move fastif(fast.next.next==head)fast=fast.next;// Set the head pointer of first halfNodehead1=head;// Set the head pointer of second halfNodehead2=slow.next;// Make second half circularfast.next=slow.next;// Make first half circularslow.next=head;return(head1,head2);}staticvoidPrintList(Nodehead){Nodecurr=head;if(head!=null){do{Console.Write(" "+curr.Data);curr=curr.next;}while(curr!=head);Console.WriteLine();}}staticvoidMain(){Nodehead1=null;Nodehead2=null;// Created linked list will be 1->2->3->4Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=head;varresult=SplitList(head);head1=result.Item1;head2=result.Item2;PrintList(head1);PrintList(head2);}}
JavaScript
classNode{constructor(newValue){this.data=newValue;this.next=null;}}// Function to split a list// into two lists.functionsplitList(head){letslow=head;letfast=head;if(head===null)return[null,null];// For odd nodes, fast->next is head and // for even nodes, fast->next->next is headwhile(fast.next!==head&&fast.next.next!==head){fast=fast.next.next;slow=slow.next;}// If there are even elements in list// then move fastif(fast.next.next===head)fast=fast.next;// Set the head pointer of first halfconsthead1=head;// Set the head pointer of second halfconsthead2=slow.next;// Make second half circularfast.next=slow.next;// Make first half circularslow.next=head;return[head1,head2];}functionprintList(head){letcurr=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->4lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=head;const[head1,head2]=splitList(head);printList(head1);printList(head2);