Zero To DSAZero To DSA
Privacy Policy
Learning Path
Big O & ComplexityArrays & StringsHashMaps & SetsLinked ListsStacks & QueuesRecursion & BacktrackingSorting & SearchingGreedy & IntervalsTrees & TriesGraphsDynamic Programming

Linked Lists

intermediate5 problems· Prerequisites: Big O & Complexity, Arrays & Strings
Learn
Practice
Exam

Linked List Basics

A linked list consists of nodes, each containing a value and a pointer to the next node (and optionally the previous node).

OperationSingly LinkedDoubly Linked
Access headO(1)O(1)
Access tailO(n)O(1)
Access by indexO(n)O(n)
Insert at headO(1)O(1)
Insert at tailO(n)O(1)
Delete by valueO(n)O(n)

When to use: Frequent insertions/deletions at the beginning, or when size is unknown. Otherwise, prefer arrays/lists.

Singly linked list nodeC#
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val = 0, ListNode next = null) {
        this.val = val;
        this.next = next;
    }
}

// C# also has built-in LinkedList<T> (doubly linked)
var list = new LinkedList<string>();
list.AddFirst("c");
list.AddLast("d");
list.AddBefore(list.First, "b");

// But for interviews, you'll usually implement the node class

Reversing a Linked List

Reversal is the most common linked list operation. The key: track three nodes — prev, current, and next — and reverse the pointer direction at each step.

Iterative reversalC#
// Reverse a singly linked list — O(n), O(1) space
ListNode ReverseList(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;

    while (curr != null) {
        ListNode next = curr.next;  // save next
        curr.next = prev;           // reverse pointer
        prev = curr;                // move prev forward
        curr = next;                // move curr forward
    }

    return prev; // new head
}

Fast & Slow Pointer

Two pointers traverse the list at different speeds. The slow pointer moves 1 step, the fast pointer moves 2 steps.

Applications:

  • Cycle detection — if fast meets slow, there's a cycle (Floyd's algorithm)
  • Find middle — when fast reaches the end, slow is at the middle
  • Find nth from end — move fast n steps ahead, then advance both
Cycle detection and find middleC#
// Detect cycle — O(n), O(1) space
bool HasCycle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast?.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) return true;
    }
    return false;
}

// Find middle node — O(n), O(1) space
ListNode FindMiddle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast?.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}

Common Interview Patterns

  1. Reverse — full reversal, reverse between positions, reverse in k-groups
  2. Merge — merge two sorted lists, merge k sorted lists
  3. Fast & slow — cycle detection, middle, palindrome check
  4. Dummy head — create a dummy node before the real head to simplify edge cases (especially with deletions)
  5. Two lists — intersection, addition (sum two numbers represented as lists)