← System Design Simulator

Reverse + Floyd Cycle Visualization for Coding Interviews

By Rahul Kumar · Senior Software Engineer · Updated · Category: Linked Lists

Iterative reversal and tortoise/hare cycle detection.

Concept

Reversing a singly linked list and detecting a cycle with Floyd's tortoise and hare are two of the most asked list manipulation questions, and they show up together because both are pure pointer gymnastics with no auxiliary allocation. Reversal is the canonical exercise in thinking in three pointers at once: previous, current, and next. Floyd's cycle detection is the canonical exercise in thinking about relative speed: two walkers, one moving twice as fast as the other, will always meet inside a cycle in O(N) time and O(1) space. Both routines run in place, never need a HashSet of visited nodes, and form building blocks for harder problems like palindrome check, reordering a list, and finding the cycle entry point. If you can produce both from memory on a whiteboard without introducing a null pointer bug, you have the fundamentals nailed.

Reverse + Floyd Cycle — Interactive Simulator

Runs fully client-side in your browser; no sign-up. Or open full screen →

Launch the interactive Reverse + Floyd Cycle visualization — animated algorithm, step-through, and live data-structure updates.

How it works

Reversal walks the list once and rewires each next pointer to point backward instead of forward. At each step, save the successor so you do not lose the rest of the list, point current.next at the previous node, shift previous to current, and shift current to the saved successor. When current becomes null, the old tail is the new head and previous is the answer. There is no sentinel, no recursion needed, no extra memory. Cycle detection uses two pointers that both start at the head. The slow pointer advances one step per iteration; the fast pointer advances two. If the list is finite and acyclic the fast pointer reaches null first and you return false. If there is a cycle, fast is running inside the loop while slow is still catching up, and the gap closes by one node per iteration until they collide. The meeting is guaranteed to happen within the length of the cycle, so the algorithm is O(N) time and O(1) space with no hash set. A subtle detail is the null check: you must test both fast and fast.next before advancing fast two steps, otherwise you dereference null when the list has even length and no cycle.

Implementation

ListNode, reverseList, and Floyd cycle detection
public class LinkedListOps {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int v) { this.val = v; }
        ListNode(int v, ListNode n) { this.val = v; this.next = n; }
    }

    /** Reverse a singly linked list in place. O(N) time, O(1) space. */
    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next; // save rest of list
            curr.next = prev;          // rewire backward
            prev = curr;               // advance prev
            curr = next;               // advance curr
        }
        return prev; // new head is the old tail
    }

    /** Floyd's tortoise and hare. True if a cycle exists. O(N) time, O(1) space. */
    public static boolean hasCycle(ListNode head) {
        if (head == null) return false;
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) return true;
        }
        return false;
    }

    /** Bonus: find the node where the cycle begins, or null. */
    public static ListNode cycleStart(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                ListNode p = head;
                while (p != slow) { p = p.next; slow = slow.next; }
                return p;
            }
        }
        return null;
    }
}

Complexity

Common pitfalls

Key design decisions & trade-offs

Practice problems

Interview follow-ups

Why it matters

Reverse + Floyd Cycle is a core part of the Linked Lists toolkit. If you are preparing for a technical interview at a top-tier company or teaching a course, a working visualization is the fastest path from "I read about it" to "I understand it".

Related