Remove Nodes From Linked List
Problem Statement
You are given the head of a linked list. Remove every node that has a strictly greater value somewhere to its right. Keep the other nodes in their original order, and return the head of what is left.
In short: a node is removed only when a bigger value sits somewhere after it. Equal values never remove each other.
head, the first node of a singly linked list.
The head of the list after the removals.
5, 2 and 3 each have a bigger value to their right (13 or 8), so they go. 13 has nothing bigger after it, and 8 is the last node, so both stay.
No value has a strictly bigger value to its right, so nothing is removed.
The 1 has a bigger 3 after it, so it goes. The first 3 stays, because the 3 after it is not strictly bigger.
- • The list has between 1 and 10^5 nodes.
- • 1 ≤ Node.val ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each node, check every node to its right for a strictly bigger value
Walk left to right; a new value pops every smaller value on the stack
Reverse, walk once keeping nodes at least as big as the running max, reverse back
Put the nodes in an array. For each node, scan all the nodes after it. If any of them is strictly bigger, the node goes. Otherwise it stays. Then link the kept nodes in their original order.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.