Split a Circular Linked List into two halves | Practice | GeeksforGeeks

Given a Cirular Linked List of size N, split it into two halves circular lists. If there are odd number of nodes in the given circular linked list then out of the resulting two halved lists, first list should have one node more than the second list. The resultant lists should also be circular lists and not linear lists.
 
Example 1:
Input:
Circular LinkedList: 1->5->7
Output:
1 5
7

 
Example 2:
Input:
Circular LinkedList: 2->6->1->5
Output:
2 6
1 5


Your Task:
Your task is to complete the given function splitList(), which takes 3 input parameters: The address of the head of the linked list, addresses of the head of the first and second halved resultant lists and Set the head1_ref and head2_ref to the first resultant list and second resultant list respectively.

Expected Time Complexity: O(N)
Expected Auxilliary Space: O(1)

Constraints:
1 <= N <= 100
+1
avinashdhn19044 days ago

void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
    Node *slow=head;
    Node *fast=head;
    Node *ptr2=NULL;
    do{
        fast=fast->next->next;
        ptr2=slow;
        slow=slow->next;
     }
    while(fast!=head&&fast->next!=head);
    Node *ptr1;
   if(fast->next==head){//case of odd number of nodes
    *head2_ref=slow->next;
     ptr1=slow->next;
    Node *ptr=slow;
    *head1_ref=head;
    ptr->next=head;
       
   }
   else{
        *head2_ref=slow;
        ptr1=slow;
          Node *ptr=ptr2;
    *head1_ref=head;
    ptr2->next=head;
   }
    
 
    while(ptr1->next!=head){
        ptr1=ptr1->next;
    }
    ptr1->next=*head2_ref;
}
+1
mahesh_phutane4 days ago
Simple java Solution
class gfg
{
        // Function  to split a circular LinkedList
	void splitList(circular_LinkedList list)
    {
        Node slow = list.head;
        Node fast = list.head;
        
        while(fast.next!=list.head && fast.next.next!=list.head){
            slow = slow.next;
            fast = fast.next.next;
        }
        if(fast.next.next==list.head){
            fast = fast.next;
        }
        list.head1 = list.head;
        list.head2 = slow.next;
        
        fast.next = slow.next; //making seconf half circular
        slow.next = list.head; //making first half circular
	 }
}
0
tusharkumar040320026 days ago
can anyone tell what's wrong with my code ?
void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
   Node*temp = head;
   int len = 0;
   while(temp)
   {
       len++;
       temp = temp->next;
       if(temp==head)break;
   }
   int half;
   *head1_ref = head;
   if(len%2==0)
   {
       half = len/2;   
   }
   else
   {
       half = (len/2)+1;
   }
   Node*prev ;
   Node*curr = head;
   int check = 0;
   while(curr)
   {
       check++;
       prev = curr;
       curr = curr->next;
       if(check==half)
       {
           *head2_ref = curr;
           prev->next = NULL;
           break;
       }
   }
   while(curr!=head)
   {
       prev = curr;
       curr = curr->next;
   }
   prev->next = NULL;
   return;
}
-1
anirudh1233212 weeks ago
void splitList(Node *head, Node **h1, Node **h2){   // your code goes here      Node* s=head;   Node* f=head;      while(f->next!=head && f->next->next!=head){       s=s->next;       f=f->next->next;   }   *h1=head;   *h2=s->next;   s->next=head;   if(f->next==head){       f->next=*h2;   }   else f->next->next=*h2;   }
0
sudhakarsinghbaghel1233211 month ago
void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
   Node *slow = head;
   Node *fast = head;
   Node *t1 = NULL;
   Node *temp = NULL;
   do
   {
      temp = slow;
      t1 = fast;
      fast = fast->next->next;
      slow = slow->next;
   } while (fast != head and fast->next != head);

   if (fast == head)//if list hv even elements,making it circular half
   {
      *head2_ref = slow;
      *head1_ref = fast;
      temp->next = fast;
      t1->next->next = slow;
   }
   else//if ll have odd elements,making it circular half
   {
      *head1_ref = head;
      *head2_ref = slow->next;
      fast->next = *head2_ref;
      slow->next = *head1_ref;
   }
}

+2
badgujarsachin831 month ago
void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
   Node* slow=head;
  Node* fast=head;
  
  while(fast->next!=head && fast->next->next!=head)
  {
      slow=slow->next;
      fast=fast->next->next;
  }
 *head1_ref=head;
 *head2_ref=slow->next;
 slow->next=head;
 if(fast->next==head)
   fast->next=*head2_ref;
 else
   fast->next->next=*head2_ref;
   
}
0
amiransarimy2 months ago
Python Solutions
 
def splitList(self, head, head1, head2):
       
       if not head:
           return None
       
       fast,slow = head,head
       
       while fast.next.next != head and fast.next != head:
           slow = slow.next
           fast = fast.next.next
           
       head2 = slow.next
       if fast.next == head:
           fast.next = head2
       else:
           fast.next.next = head2
       
       head1 = head
       slow.next = head
       #this is to emulate pass by reference in python please don't delete below line.
       return head1,head2
-2
as0042302 months ago
In C++;
void splitList(Node *head, Node **head1_ref, Node **head2_ref){  Node *slow = head;  Node *fast = head;    while(fast->next->next != head && fast->next != head){      slow = slow->next;      fast = fast->next->next;  }    *head2_ref = slow->next;  if(fast->next == head) fast->next = *head2_ref;  else fast->next->next = *head2_ref;  *head1_ref = head;   slow->next = head;}
-1
shailendramaheshwari172 months ago
void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
   // your code goes here
   
   Node *fast = head , *slow =head;
   
   while(fast->next!=head && fast->next->next!=head)
   {
       fast=fast->next->next;
       slow=slow->next;
   }
   if(fast->next == head)
   {
       fast->next = slow->next;
       *head2_ref = slow->next;
       *head1_ref = slow->next = head;
   }
   else if(fast->next->next == head)
   {
       fast->next->next = slow->next;
       *head2_ref = slow->next;
       *head1_ref = slow->next = head;
   }
}
-2
priyeshanand92 months ago
void splitList(Node *head, Node **head1_ref, Node **head2_ref){   int c=0;   Node *p=head, *q=head;   while(p){       c++;       if(p->next==head) break;       p=p->next;   }   if(c%2==1) c=(c/2)+1;   else c=c/2;   int d=0;   while(d<=c){       d++;       if(d==1)       *head1_ref=q;       if(d==c){           *head2_ref=q->next;           q->next=*head1_ref;       }       q=q->next;   }   Node *r=*head2_ref;   while(1){       if(r->next==head){           r->next=*head2_ref;           break;       }       r=r->next;   }}
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.