Reversing & Detecting Cycles in Linked Lists - DSA Tutorial

Last modified - 15-08-2026

Author - Krishna Shinde


If you've been working through a DSA tutorial on linked lists, you've probably noticed that almost every linked list interview question boils down to one of two skills: reversing a linked list or detecting a cycle in a linked list. Once these two patterns click, a huge chunk of linked list problems suddenly feel a lot more manageable.

In this tutorial, we'll break down both patterns step by step, with clean C++ code, manual walkthroughs, and the interview context you need to actually use them with confidence.

Table of Contents

Why These Two Patterns Matter

Linked lists don't support random access the way arrays do, so most linked list problems are really about pointer manipulation. Reversing tests whether you can rewire next pointers without losing track of the list. Cycle detection tests whether you can reason about two pointers moving at different speeds through the same structure.

Master these two, and problems like "reverse in groups of k," "find the middle of a linked list," and "detect and remove a loop" stop feeling like separate topics; they're just variations on the same two ideas.

1. Reversing a Linked List

Reversing a linked list means flipping the direction of every next pointer, so the last node becomes the head and the original head becomes the tail.

reversing a linked list

How Iterative Reversal Works

  1. Keep track of three pointers: prev, curr, and next.
  2. Start with prev = nullptr and curr = head.
  3. Before changing anything, save curr->next in next, so you don't lose the rest of the list.
  4. Point curr->next backward to prev.
  5. Move prev and curr one step forward.
  6. Repeat until curr becomes nullptr. At that point, prev is the new head.

Iterative Reversal in C++

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;

    Node(int val) : data(val), next(nullptr) {}
};

Node* reverseList(Node* head) {
    Node* prev = nullptr;
    Node* curr = head;

    while (curr != nullptr) {
        Node* next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }

    return prev;
}

void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data << " ";
        head = head->next;
    }

    cout << endl;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);

    Node* reversed = reverseList(head);
    printList(reversed);

    return 0;
}

Output:

4 3 2 1

Manual Walkthrough

Let's trace reversal on 1 -> 2 -> 3 -> nullptr:

The Recursive Approach

Reversal can also be done recursively, which is a common interview follow-up question:

Node* reverseListRecursive(Node* head) {

    if (head == nullptr || head->next == nullptr) {

        return head;

    }

    Node* newHead = reverseListRecursive(head->next);

    head->next->next = head;

    head->next = nullptr;

    return newHead;

}

The recursive version reverses the rest of the list first, then fixes the current node's link on the way back up the call stack.

2. Detecting Cycles in a Linked List

A cycle happens when a node's next pointer points back to an earlier node instead of nullptr, causing the list to loop forever. This is where Floyd's Cycle Detection Algorithm, also called the "tortoise and hare" technique, comes in.


detect a cycle in linked list

How Floyd's Algorithm Works

  1. Use two pointers: slow, which moves one step at a time, and fast, which moves two steps at a time.
  2. If there's no cycle, fast reaches nullptr first, and you can safely conclude the list is cycle-free.
  3. If there is a cycle, fast and slow will eventually land on the same node, since fast keeps "lapping" slow inside the loop.
  4. The moment slow == fast, a cycle is confirmed.

Cycle Detection in C++

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;

    Node(int val) : data(val), next(nullptr) {}
};

bool hasCycle(Node* head) {
    Node* slow = head;
    Node* fast = head;

    while (fast != nullptr && fast->next != nullptr) {
        slow = slow->next;
        fast = fast->next->next;

        if (slow == fast) {
            return true;
        }
    }

    return false;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = head->next; // creates a cycle

    cout << (hasCycle(head) ? "Cycle detected" : "No cycle") << endl;

    return 0;
}

Output:

Cycle detected

Manual Walkthrough

Take the list 1 -> 2 -> 3 -> 2 (node 3 points back to node 2, forming a loop):

Finding Where the Cycle Starts

Once slow and fast meet, resetting one pointer to head and moving both one step at a time until they meet again will land you exactly on the node where the cycle begins. This extension is a favorite in senior-level interviews because it tests whether you truly understand the math behind the algorithm, not just the code.

Time & Space Complexity

OperationTime ComplexitySpace Complexity
Reverse a Linked List (Iterative)O(n)O(1)
Reverse a Linked List (Recursive)O(n)O(n) (call stack)
Detect a Cycle (Floyd's Algorithm)O(n)O(1)
Detect a Cycle (Hashing Approach)O(n)O(n)

Floyd's algorithm is the standard answer in interviews specifically because it detects a cycle in constant space, unlike the hashing approach, which stores visited nodes in a set.

When Do These Patterns Actually Show Up?

If you're prepping for interviews, these aren't optional topics — they're two of the highest-frequency linked list patterns you'll be asked to apply live.

Conclusion

Reversing a linked list and detecting cycles both come down to careful pointer tracking: one rewires links to flip direction, the other uses two pointers moving at different speeds to catch a loop. Once you've traced through both by hand and written the code yourself, these stop being separate tricks and start feeling like the same underlying skill applied two different ways.

We've already covered linked list fundamentals in our previous article — explore our full DSA tutorial series for more. Follow Algoflame for more programming content.