700130. Split a Linked List into two halves

Easy

Split a Linked List into two halves

Summary

This problem is about splitting a circular linked list into two halves. A circular linked list is a type of linked list where the last node points back to the first node, forming a circle. The goal is to divide the list into two halves such that each half contains roughly the same number of nodes.

Detailed Explanation

To solve this problem, we can start by assuming we have a pointer at the beginning of the circular linked list. We then move the pointer along the list until it reaches the middle node (or the node just before the middle node if the total number of nodes is odd).

Next, we set the second pointer to the starting point and move it forward until it meets the first pointer. This will give us two halves of roughly equal length.

Here's a step-by-step breakdown:

  1. Initialize two pointers: one at the beginning of the list (p1) and another at the starting point (p2).
  2. Move p1 along the list until it reaches the middle node (or the node just before the middle node if the total number of nodes is odd).
  3. Set p2 to the starting point.
  4. Move p2 forward until it meets p1. This will give us two halves of roughly equal length.

Time complexity: O(n), where n is the total number of nodes in the list.

Space complexity: O(1), as we only use a constant amount of space to store the pointers.

Optimized Solutions

Java

View on GeeksforGeeks
public class Solution {
    Node* splitList(Node* head) {
        if (head == null || head.next == head) return head;
        
        Node* slow = head;
        Node* fast = head;
        while (fast.next != head && fast.next.next != head) {
            slow = slow.next;
            fast = fast.next.next;
        }
        
        Node* newHead = slow.next;
        slow.next = null;
        return head;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.