Tech Interview RoomStudy guides

Linked List

Reverse a linked list, and detect a cycle in it.

Intermediate · 2 min read · Lesson 2 of 5 in Linked List

Problem

  1. Reverse a singly linked list iteratively and recursively.
  2. Detect whether a list has a cycle and find where the cycle starts.

Approach

  • Iterative reversal: walk the list re-pointing next to the previous node, using three pointers.
  • Floyd's tortoise and hare: a slow pointer moves 1 step and a fast pointer 2 steps; if they meet, there is a cycle. To find the cycle start, reset one pointer to the head and move both 1 step at a time; they meet at the start of the cycle.
java
class LinkedListOps {
    static final class Node {
        int val;
        Node next;
        Node(int val) { this.val = val; }
    }

    static Node reverse(Node head) {
        Node prev = null, curr = head;
        while (curr != null) {
            Node next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    static Node reverseRecursive(Node head) {
        if (head == null || head.next == null) return head;
        Node newHead = reverseRecursive(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    static Node cycleStart(Node head) {
        Node slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {                 // cycle found
                Node p = head;
                while (p != slow) { p = p.next; slow = slow.next; }
                return p;                       // start of the cycle
            }
        }
        return null;                            // no cycle
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        Node r = reverse(head);
        System.out.println(r.val + " " + r.next.val + " " + r.next.next.val);   // 3 2 1

        Node a = new Node(1), b = new Node(2), c = new Node(3), d = new Node(4);
        a.next = b; b.next = c; c.next = d; d.next = b;                          // cycle starts at 2
        System.out.println(cycleStart(a).val);                                   // 2
    }
}

Complexity

Reversal: O(n) time, O(1) space iteratively (O(n) stack recursively). Cycle detection: O(n) time, O(1) space.

Follow-up questions interviewers ask

Try answering each one before you open it.

Why does resetting one pointer to the head find the cycle start?

Let the distance from head to cycle start be a, and the meeting point be b steps into the cycle of length c. Slow travelled a + b, fast travelled 2(a + b), and the difference a + b is a multiple of c. So walking a more steps from the meeting point lands exactly at the cycle start, the same distance as from the head.

How would you find the middle node in one pass?

Move slow by one and fast by two; when fast reaches the end, slow is at the middle. The same technique finds the kth node from the end (start the fast pointer k nodes ahead) and checks for palindromes (find the middle, reverse the second half, compare).