Pattern 09 of 22

Linked List Pattern: Template + 75 LeetCode Problems

Rewire pointers in place, with a dummy head and a saved next to keep it safe.

Easy
12
Medium
56
Hard
7
Time
O(n)

What the linked list pattern is

Linked list problems are pointer-rewiring exercises, and nearly all of the difficulty is in the edge cases rather than the algorithm. Two habits remove most of them. First, allocate a dummy node in front of the real head whenever the head itself might be deleted or replaced, so the head stops being a special case and the answer is always `dummy.next`. Second, before overwriting any `next`, save it — the reversal loop is exactly the three-step dance of saving the successor, redirecting the current node backwards, and stepping both cursors forward. The other half of the pattern is fast-and-slow pointers: advancing one cursor two nodes for every one of the other's finds the midpoint in a single pass, detects a cycle when the two meet, and locates the cycle's entry point when a third walk starts from the head. In-place work keeps the space at O(1), which is usually the actual constraint being tested.

When to use it

  • Nodes must be reordered, reversed, merged or removed without allocating a new list.
  • The head of the list can change — that is the dummy node's cue.
  • A cycle, a midpoint, or the nth node from the end is wanted in one pass.
  • The problem forbids converting the list to an array, which is otherwise always tempting.

The linked list template in Python

The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 75 problems listed below.

PythonLinked List template
def reverse(head):
    previous = None
    node = head

    while node:
        following = node.next    # save it before the link is destroyed
        node.next = previous     # rewire backwards
        previous = node          # step both cursors forward
        node = following

    return previous              # the old tail is the new head

Complexity characteristics

Time
O(n)
Auxiliary space
O(1)

One pass, rewiring pointers as it goes. The constant space is the entire point of the pattern and is usually the constraint being tested — copying the list into an array makes almost all of these problems trivial at O(n) space, which is why the statement so often forbids it. Fast-and-slow pointers hold both figures: the midpoint, a cycle check and the nth node from the end each cost one pass and two pointers.

Eyes only

Meet the linked list problem you did not practise.

In the live round, Stealth Interview reads the problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer says. It's a desktop app for macOS and Windows, invisible to screen sharing.

All 75 linked list LeetCode problems

Every problem in the library the linked list pattern applies to, grouped by LeetCode's own difficulty rating. 63 of the 75 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.

Related LeetCode topics

Easy (12)

Easy problems
No.Problem
0021Merge Two Sorted Lists
0083Remove Duplicates from Sorted List
0141Linked List Cycle
0160Intersection of Two Linked Lists
0203Remove Linked List Elements
0206Reverse Linked List
0234Palindrome Linked List
0705Design HashSet
0706Design HashMap
0876Middle of the Linked List
1290Convert Binary Number in a Linked List to Integer
1474Delete N Nodes After M Nodes of a Linked ListPremium

Medium (56)

Medium problems
No.Problem
0002Add Two Numbers
0019Remove Nth Node From End of List
0024Swap Nodes in Pairs
0061Rotate List
0082Remove Duplicates from Sorted List II
0086Partition List
0092Reverse Linked List II
0109Convert Sorted List to Binary Search Tree
0114Flatten Binary Tree to Linked List
0116Populating Next Right Pointers in Each Node
0117Populating Next Right Pointers in Each Node II
0138Copy List with Random Pointer
0142Linked List Cycle II
0143Reorder List
0146LRU Cache
0147Insertion Sort List
0148Sort List
0237Delete Node in a Linked List
0328Odd Even Linked List
0355Design Twitter
0382Linked List Random Node
0430Flatten a Multilevel Doubly Linked List
0445Add Two Numbers II
0622Design Circular Queue
0641Design Circular Deque
0707Design Linked List
0725Split Linked List in Parts
2095Delete the Middle Node of a Linked List
2130Maximum Twin Sum of a Linked List
0369Plus One Linked ListPremium
0379Design Phone DirectoryPremium
0426Convert Binary Search Tree to Sorted Doubly Linked ListPremium
0708Insert into a Sorted Circular Linked ListPremium
0817Linked List Components
1019Next Greater Node In Linked List
1171Remove Zero Sum Consecutive Nodes from Linked List
1265Print Immutable Linked List in ReversePremium
1367Linked List in Binary Tree
1472Design Browser History
1634Add Two Polynomials Represented as Linked ListsPremium
1669Merge In Between Linked Lists
1670Design Front Middle Back Queue
1721Swapping Nodes in a Linked List
1756Design Most Recently Used QueuePremium
1797Design Authentication Manager
1836Remove Duplicates From an Unsorted Linked ListPremium
2046Sort Linked List Already Sorted Using Absolute ValuesPremium
2058Find the Minimum and Maximum Number of Nodes Between Critical Points
2074Reverse Nodes in Even Length Groups
2181Merge Nodes in Between Zeros
2289Steps to Make Array Non-decreasing
2326Spiral Matrix IV
2487Remove Nodes From Linked List
2674Split a Circular Linked ListPremium
2807Insert Greatest Common Divisors in Linked List
2816Double a Number Represented as a Linked List

Hard (7)

Hard problems
No.Problem
0023Merge k Sorted Lists
0025Reverse Nodes in k-Group
0432All O`one Data Structure
0460LFU Cache
0716Max StackPremium
1206Design Skiplist
2296Design a Text Editor

Linked List pattern FAQ

What is the linked list pattern?

Linked list problems are pointer-rewiring exercises, and nearly all of the difficulty is in the edge cases rather than the algorithm. Two habits remove most of them.

How many LeetCode problems use the linked list pattern?

This page lists 75 LeetCode problems that the linked list pattern applies to: 12 Easy, 56 Medium and 7 Hard. 63 of them carry a complete Python solution with complexity analysis.

What is the time complexity of the linked list pattern?

O(n) time and O(1) space. One pass, rewiring pointers as it goes. The constant space is the entire point of the pattern and is usually the constraint being tested — copying the list into an array makes almost all of these problems trivial at O(n) space, which is why the statement so often forbids it. Fast-and-slow pointers hold both figures: the midpoint, a cycle check and the nth node from the end each cost one pass and two pointers.

When should I use the linked list pattern in an interview?

Nodes must be reordered, reversed, merged or removed without allocating a new list. The head of the list can change — that is the dummy node's cue.

Which linked list problem should I start with?

LeetCode 21. Merge Two Sorted Lists is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.

What patterns are related to linked list?

Two Pointers, Hash Map, Tree Traversal, Sorting. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.

More ways in: all 22 patterns, the curated study lists, or the full problem list.