Split a Circular Linked List - Problem
Given a circular linked list list of positive integers, your task is to split it into 2 circular linked lists so that the first one contains the first half of the nodes in list (exactly ceil(list.length / 2) nodes) in the same order they appeared in list, and the second one contains the rest of the nodes in list in the same order they appeared in list.
Return an array answer of length 2 in which the first element is a circular linked list representing the first half and the second element is a circular linked list representing the second half.
A circular linked list is a normal linked list with the only difference being that the last node's next node, is the first node.
Input & Output
Example 1 — Odd Number of Nodes
$
Input:
head = [1,2,3,4,5]
›
Output:
[[1,2,3],[4,5]]
💡 Note:
The list has 5 nodes. First half gets ceil(5/2) = 3 nodes: [1,2,3]. Second half gets remaining 2 nodes: [4,5]. Both halves are made circular.
Example 2 — Even Number of Nodes
$
Input:
head = [1,2,3,4]
›
Output:
[[1,2],[3,4]]
💡 Note:
The list has 4 nodes. First half gets ceil(4/2) = 2 nodes: [1,2]. Second half gets remaining 2 nodes: [3,4]. Both are circular.
Example 3 — Single Node
$
Input:
head = [1]
›
Output:
[[1],[]]
💡 Note:
Only one node, so first half contains [1] and second half is empty. The single node points to itself.
Constraints
- 1 ≤ list.length ≤ 105
- 1 ≤ list[i] ≤ 106
- The input list is a circular linked list
Visualization
Tap to expand
Understanding the Visualization
1
Input
Circular linked list [1,2,3,4,5] where 5 points back to 1
2
Find Split Point
Use two-pointer technique or count nodes to find middle
3
Output
Two circular lists: [1,2,3] and [4,5]
Key Takeaway
🎯 Key Insight: Use fast-slow pointers to find the split point in one traversal
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code