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
Split Circular Linked List: [1,2,3,4,5] → Two Circular HalvesInput: One circular list → Output: Two circular lists12345Split into two circular listsSPLITFirst Half: [1,2,3]ceil(5/2) = 3 nodesCircular: 3 → 1Second Half: [4,5]Remaining 2 nodesCircular: 5 → 4
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
Asked in
Microsoft 15 Amazon 12 Facebook 8
28.5K Views
Medium Frequency
~25 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen