Split a Circular Linked List - Problem
Split a Circular Linked List
Imagine you have a circular linked list where the last node points back to the first node, forming an endless loop. Your task is to split this circular structure into two separate circular linked lists.
The first circular list should contain the first half of the nodes (exactly
Input: Head of a circular linked list with positive integers
Output: Array of two circular linked lists representing the split halves
Example: If you have a circular list [1→2→3→4→1], you should return [[1→2→1], [3→4→3]]
Imagine you have a circular linked list where the last node points back to the first node, forming an endless loop. Your task is to split this circular structure into two separate circular linked lists.
The first circular list should contain the first half of the nodes (exactly
ceil(n/2) nodes), and the second circular list should contain the remaining nodes. Both resulting lists must maintain their circular property and preserve the original order of nodes.Input: Head of a circular linked list with positive integers
Output: Array of two circular linked lists representing the split halves
Example: If you have a circular list [1→2→3→4→1], you should return [[1→2→1], [3→4→3]]
Input & Output
example_1.py — Basic Even Length List
$
Input:
Circular list: [1,2,3,4] → 1 (4 nodes)
›
Output:
[[1,2], [3,4]] where both are circular
💡 Note:
With 4 nodes, first half gets ceil(4/2) = 2 nodes [1,2], second half gets remaining 2 nodes [3,4]. Both maintain circular structure.
example_2.py — Odd Length List
$
Input:
Circular list: [1,2,3,4,5] → 1 (5 nodes)
›
Output:
[[1,2,3], [4,5]] where both are circular
💡 Note:
With 5 nodes, first half gets ceil(5/2) = 3 nodes [1,2,3], second half gets remaining 2 nodes [4,5]. The extra node goes to the first half.
example_3.py — Single Node Edge Case
$
Input:
Circular list: [1] → 1 (1 node)
›
Output:
[[1], null]
💡 Note:
Single node forms the first circular list, second list is null since there are no remaining nodes.
Constraints
- The number of nodes in the list is in the range [1, 105]
- 1 ≤ Node.val ≤ 106
- The given list is guaranteed to be circular
- You must maintain the original order of nodes in both resulting lists
Visualization
Tap to expand
Understanding the Visualization
1
Identify the Circle
Start with a circular linked list where the last node points back to the first
2
Find the Middle
Use fast/slow pointers to locate the exact split point efficiently
3
Break and Reconnect
Carefully break the circle at the middle and form two new circular lists
4
Verify Circles
Ensure both resulting lists maintain their circular property
Key Takeaway
🎯 Key Insight: The fast and slow pointer technique eliminates the need to count nodes first, finding the optimal split point in a single pass through the circular list.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code