# Split a Circular Linked List into two halves

`                         Original Linked List  `

`                         Result Linked List 1  `

`                         Result Linked List 2  `

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Thanks to Geek4u for suggesting the algorithm.
1) Store the mid and last pointers of the circular linked list using tortoise and hare algorithm.
2) Make the second half circular.
3) Make the first half circular.

In the below implementation, if there are odd nodes in the given circular linked list then the first result list has 1 more node than the second result list.

## C

```/* Program to split a circular linked list into two halves */
#include<stdio.h>
#include<stdlib.h>

/* structure for a node */
struct Node
{
int data;
struct Node *next;
};

/* Function to split a list (starting with head) into two lists.
the two resultant linked lists */
{

return;

/* If there are odd nodes in the circular list then
fast_ptr->next becomes head and for even nodes
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}

/* If there are even elements in list then move fast_ptr */
fast_ptr = fast_ptr->next;

/* Set the head pointer of first half */

/* Set the head pointer of second half */

/* Make second half circular */
fast_ptr->next = slow_ptr->next;

/* Make first half circular */
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the begining of a Circular
void push(struct Node **head_ref, int data)
{
struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node));
ptr1->data = data;

/* If linked list is not NULL then set the next of
last node */
{
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */

}

/* Function to print nodes in a given Circular linked list */
{
{
printf("\n");
do {
printf("%d ", temp->data);
temp = temp->next;
}
}

/* Driver program to test above functions */
int main()
{
int list_size, i;

/* Initialize lists as empty */

/* Created linked list will be 12->56->2->11 */

/* Split the list */

getchar();
return 0;
}
```

## Java

```// Java program to delete a node from doubly linked list

static class Node {

int data;
Node next, prev;

Node(int d) {
data = d;
next = prev = null;
}
}

/* Function to split a list (starting with head) into two lists.
the two resultant linked lists */
void splitList() {

return;
}

/* If there are odd nodes in the circular list then
fast_ptr->next becomes head and for even nodes
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}

/* If there are even elements in list then move fast_ptr */
fast_ptr = fast_ptr.next;
}

/* Set the head pointer of first half */

/* Set the head pointer of second half */
}
/* Make second half circular */
fast_ptr.next = slow_ptr.next;

/* Make first half circular */
}

/* Function to print nodes in a given singly linked list */
void printList(Node node) {
Node temp = node;
if (node != null) {
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != node);
}
}

public static void main(String[] args) {

//Created linked list will be 12->56->2->11

// Split the list
list.splitList();
System.out.println("");
System.out.println("First Circular List ");
System.out.println("");
System.out.println("Second Circular List ");

}
}

// This code has been contributed by Mayank Jaiswal

```

## Python

```
# Python program to split circulart linked list into two halves

# A node structure
class Node:

# Constructor to create a new node
def __init__(self, data):
self.data = data
self.next = None

# Class to create a new  Circular Linked list

# Constructor to create a empty circular linked list
def __init__(self):

# Function to insert a node at the beginning of a
def push(self, data):
ptr1 = Node(data)

# If linked list is not None then set the next of
# last node
temp = temp.next
temp.next = ptr1

else:
ptr1.next = ptr1 # For the first node

# Function to print nodes in a given circular linked list
def printList(self):
while(True):
print "%d" %(temp.data),
temp = temp.next
break

# Function to split a list (starting with head) into

return

# If htere are odd nodes in the circular list then
# fast_ptr->next becomes head and for even nodes
fast_ptr = fast_ptr.next.next
slow_ptr = slow_ptr.next

# If there are event elements in list then
# move fast_ptr
fast_ptr = fast_ptr.next

# Set the head pointer of first half

# Set the head pointer of second half

# Make second half circular
fast_ptr.next = slow_ptr.next

# Make first half circular

# Driver program to test above functions

# Initialize lists as empty

# Split the list

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

Output:

```Original Circular Linked List
11 2 56 12
11 2
56 12
```

Time Complexity: O(n)

Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem

# GATE CS Corner    Company Wise Coding Practice

2.2 Average Difficulty : 2.2/5.0
Based on 125 vote(s)