Tech Interview RoomStudy guides

Linked List

Remove the Nth node from the end of a linked list. Compare two passes, a list of nodes, and one pass with two pointers.

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

Problem

1 → 2 → 3 → 4 → 5, n = 2 → 1 → 2 → 3 → 5.

Pattern

Fast/slow pointers with a fixed gap: move fast n steps ahead; then move both until fast reaches the end, and slow is just before the node to remove. A dummy head handles removing the first node.

java
final class Node {
    int val;
    Node next;
    Node(int val, Node next) { this.val = val; this.next = next; }
    static Node of(int... v) { Node h = null; for (int i = v.length - 1; i >= 0; i--) h = new Node(v[i], h); return h; }
    static String show(Node n) { StringBuilder sb = new StringBuilder(); for (; n != null; n = n.next) sb.append(n.val).append(' '); return sb.toString().trim(); }
}

Approach 1: Store nodes in a list (O(n) time, O(n) space)

java
import java.util.ArrayList;
import java.util.List;

class RemoveNthList {
    static Node remove(Node head, int n) {
        List<Node> nodes = new ArrayList<>();
        for (Node x = head; x != null; x = x.next) nodes.add(x);
        int idx = nodes.size() - n;
        if (idx == 0) return head.next;
        nodes.get(idx - 1).next = nodes.get(idx).next;
        return head;
    }
}

Approach 2: Two passes: count the length, then walk to length − n (O(n), O(1))

java
class RemoveNthTwoPass {
    static Node remove(Node head, int n) {
        int len = 0;
        for (Node x = head; x != null; x = x.next) len++;
        Node dummy = new Node(0, head), prev = dummy;
        for (int i = 0; i < len - n; i++) prev = prev.next;
        prev.next = prev.next.next;
        return dummy.next;
    }
}

Approach 3: One pass with a gap of n between two pointers (O(n), O(1)) — optimal

java
class RemoveNthOnePass {
    static Node remove(Node head, int n) {
        Node dummy = new Node(0, head), fast = dummy, slow = dummy;
        for (int i = 0; i <= n; i++) fast = fast.next;           // gap of n + 1 nodes
        while (fast != null) { fast = fast.next; slow = slow.next; }
        slow.next = slow.next.next;
        return dummy.next;
    }
}

class RemoveNthDemo {
    public static void main(String[] args) {
        System.out.println(Node.show(RemoveNthList.remove(Node.of(1, 2, 3, 4, 5), 2)) + " | "
            + Node.show(RemoveNthTwoPass.remove(Node.of(1, 2, 3, 4, 5), 2)) + " | "
            + Node.show(RemoveNthOnePass.remove(Node.of(1, 2, 3, 4, 5), 2)));     // 1 2 3 5 x3
        System.out.println("[" + Node.show(RemoveNthOnePass.remove(Node.of(1), 1)) + "] " + Node.show(RemoveNthOnePass.remove(Node.of(1, 2), 2)));  // [] 2
    }
}

Feasible → optimal

List of nodes O(n) space → two passes O(1) space → one pass O(1) space. All O(n) time; the one-pass version is the expected answer.

Follow-up questions interviewers ask

Try answering each one before you open it.

Related (same pattern): find the middle node.

Move slow one step and fast two steps; when fast reaches the end, slow is the middle (second middle for even lengths). O(n), O(1).

Related (same pattern): rotate a list right by k places.

Compute the length L and connect the tail to the head; the new tail is at position L - k % L - 1 from the start; break the ring after it. O(n). Fast/slow pointers with a gap of k % L also find the new tail in one extra pass.