Stop 10 · Data Structures & Algorithms in JavaTopics · 6/11

Linked Lists

Part of the Java Roadmap

In short

Linked lists are rarely the right choice in real Java code, but they are a favourite in interviews because they test how carefully you handle pointers, nulls and edge cases.

A linked list is a chain of nodes that point to the next node. Interview problems include reversing a list, finding the middle, detecting cycles and merging sorted lists.

The jaadu in a railway yard strains to hook a heavy coupling between two train coaches standing on the tracks.

Think of it like this

Coupling train coaches

Each coach of the Rajdhani is only coupled to the one behind it. To reach coach B7 you start at the engine and walk through. Adding a coach in the middle just means uncoupling two and hooking a new one between them. To reverse the train, you flip every coupling one by one.

Watch it happen

Reversing 1 → 2 → 3 → 4

1
0
curr
2
1
3
2
4
3

1/5prev starts as null and curr at the head, node 1.

The big ideas

What you need to know

Nodes and next

Each node holds a value and a reference to the next node. The last one points to null.

Trade-offs

Insert or delete at a known node is O(1). Reaching index i is O(n), because there is no direct access like an array.

Slow and fast pointers

Move one pointer one step and another two steps. Use it to find the middle or detect a cycle.

Dummy head trick

A fake node before the head removes special cases when merging lists or deleting the first node.

See it in code

Walk through the code

java
1public class LinkedListProblems {2    static class Node {3        int val;4        Node next;5        Node(int val) { this.val = val; }6    }7 8    static Node reverse(Node head) {9        Node prev = null, curr = head;10        while (curr != null) {11            Node next = curr.next;   // save the rest12            curr.next = prev;        // flip the arrow13            prev = curr;             // move prev forward14            curr = next;             // move curr forward15        }16        return prev;                 // new head17    }18 19    static Node middle(Node head) {20        Node slow = head, fast = head;21        while (fast != null && fast.next != null) {22            slow = slow.next;23            fast = fast.next.next;24        }25        return slow;26    }27 28    static boolean hasCycle(Node head) {29        Node slow = head, fast = head;30        while (fast != null && fast.next != null) {31            slow = slow.next;32            fast = fast.next.next;33            if (slow == fast) return true;34        }35        return false;36    }37}

1/4A node is just a value and a reference to the next node.

The three linked list problems you are most likely to be asked: reverse, middle, and cycle detection.

Save yourself the pain

Common mistakes

Oops

Writing curr.next = prev before saving curr.next.

Do this instead

Always store Node next = curr.next first, otherwise the rest of the list is gone.

Oops

Checking only fast.next != null in the loop.

Do this instead

Check fast != null && fast.next != null, or a list with an even length throws NullPointerException.

Oops

Using java.util.LinkedList in real code because inserts are O(1).

Do this instead

ArrayList or ArrayDeque are faster in practice for almost every workload.

Your mission

Pointer practice

~60 min

Quick check

What is the time complexity of reading the 500th element of a singly linked list?

Go deeper

  1. 1Implement a singly linked list with your own Node class.
  2. 2Reverse a linked list iteratively and recursively.
  3. 3Detect a cycle with the fast and slow pointer technique.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge