Split a Circular linked list into two halves

Pattern: Doubly and circular lists

medium

Find the middle and the last node with slow and fast pointers that stop before coming back to the head. Then close the first half into its own circle and the second half into another.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Given the head of a circular linked list with at least two nodes, split it into two circular lists of equal size. If the count is odd, the first list gets the extra node. Return both heads. For example, the circle 1 5 7 gives circles 1 5 and 7, and 2 6 1 5 gives 2 6 and 1 5.

This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name splitList and the return form (an array of two heads in Java, a pair in C++, a tuple in Python) are this guide's own.

Think first

  • How do you find the middle of a list that has no null at the end?
  • Which node ends each half?
  • Which two pointers change?

Hints

    Brute force, and why it fails

    Count the nodes in one lap, then walk to the middle: two passes, still O(n). Slow and fast pointers find the middle and the last node in one pass.

    The key insight

    A circle's "end" is the node before the head. Replacing "until null" with "until back at the head" lets the usual middle search work on circles.

    Dry run

    20slow, fast
    61 
    12 
    53 

    The circular list is 2 → 6 → 1 → 5, and 5 points back to 2. slow and fast start on 2.

    Step 1 of 4

    The plan

    slow = fast = head
    while fast.next != head and fast.next.next != head:
        slow = slow.next; fast = fast.next.next
    if fast.next.next == head: fast = fast.next
    second = slow.next
    slow.next = head
    fast.next = second
    return head, second
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One pass round the circle.

    Edge cases

    • Two nodes: each becomes a circle of one.
    • Odd counts: the first half is one longer.
    • Long circles: still one pass.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode[] splitList(ListNode head) {
            ListNode slow = head, fast = head;      // fast moves two steps; it stops at the last or second-to-last node
            while (fast.next != head && fast.next.next != head) {
                slow = slow.next;
                fast = fast.next.next;
            }
            if (fast.next.next == head) fast = fast.next;   // even length: move fast to the last node
            ListNode second = slow.next;
            slow.next = head;                       // close the first half into a circle
            fast.next = second;                     // close the second half into a circle
            return new ListNode[]{head, second};
        }
    }

    C++

    class Solution {
    public:
        pair<ListNode*, ListNode*> splitList(ListNode* head) {
            ListNode *slow = head, *fast = head;    // fast moves two steps; it stops at the last or second-to-last node
            while (fast->next != head && fast->next->next != head) {
                slow = slow->next;
                fast = fast->next->next;
            }
            if (fast->next->next == head) fast = fast->next;   // even length: move fast to the last node
            ListNode* second = slow->next;
            slow->next = head;                      // close the first half into a circle
            fast->next = second;                    // close the second half into a circle
            return make_pair(head, second);
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def splitList(self, head: ListNode) -> tuple:
            slow = fast = head                      # fast moves two steps; it stops at the last or second-to-last node
            while fast.next is not head and fast.next.next is not head:
                slow, fast = slow.next, fast.next.next
            if fast.next.next is head:
                fast = fast.next                    # even length: move fast to the last node
            second = slow.next
            slow.next = head                        # close the first half into a circle
            fast.next = second                      # close the second half into a circle
            return head, second

    Common mistakes

    • Stopping on null, which never comes in a circle.
    • Forgetting to move fast to the last node for even counts.
    • Closing only one of the two circles.

    Similar problems