# C++ program to split a circular linked list into two halves ## How to split a circular liked list into two halves in C++?

To write a C++ program to split a circular linked list into two halves, we will be finding the middle of the given list first. In order to find the middle of the list we will use the slow and fast pointer trick. And after finding the middle of the list we will make two heads in the list as each part of the list will have its own head. Further in this article we will discuss steps and algorithm two find the middle of the list and make two heads and divide the list into two halves.

## Steps to split a circular linked list into two halves in CPP

2. Create some basic functions like to build and display the circular liked list.
3. Now create a function to split the list into two halves.
4. In this function find the middle of the list using the slow and fast pointer trick.
5. Now when list will be divided into two halves there will be two heads.
6. So, make the node pointed by fast pointer as first head.
7. Make the node pointed by slow pointer as second head.
8. Display both the halves to the user.
```struct node {
int num;
struct node * next;
}``` ## Algorithm  to split circular linked list into two halves in C++

• STRUCT NODE SLOW = HEAD
• STRUCT NODE FAST = HEAD
• RETURN
• FAST=FAST->NEXT->NEXT
• SLOW=SLOW->NEXT
• END WHILE
• FAST=FAST->NEXT
• FAST->NEXT=SLOW->NEXT

## C++ program for splitting the circular linked list into two halves

Run
```#include<iostream>
using namespace std;

struct Node
{
int num;
struct Node *next;

void insertStart (struct Node **head, int data)	//function to create linked list
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->num = data;

// if its the first node being entered
{

cout << newNode->num << " Inserted\n";
return;
}

// if CLL already as >=1 node

// traverse till last node in CLL
{
curr = curr->next;
}

curr->next = newNode;		// last node's next as this new node

cout << newNode->num << " Inserted\n";

// previous head node becomes 2nd node
}

{

//cout << "\nCircular Linked List : " << endl;

// if circular linked list is empty currently
return;

// since we need to take care of circular nature of linked list
do
{
cout << temp->num << " ";
temp = temp->next;

}
cout << endl;
}

void splitList (struct Node **head1, struct Node **head2)	//function to split list into two halves
{

return;

while (fast->next != head && fast->next->next != head)	//finding middle of the list
{
fast = fast->next->next;
slow = slow->next;
}

fast = fast->next;

fast->next = slow->next;

}

int main ()				//main function
{

cout << "\nCircular linked list data:\n";
cout << "\nFirst half data: ";
cout << "\nSecond half data: ";
return 0;
}
```
```Output:
1 Inserted
2 Inserted
3 Inserted
4 Inserted
5 Inserted

5 4 3 2 1

First half data: 5 4 3

Second half data: 2 1 ```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java