If prev.next.val == val, bypass it with prev.next = prev.next.next. Notice prev does not move forward, because the new prev.next might also equal val.
Otherwise, advance prev = prev.next.
Return dummy.next.
04Watch it work
Visualizer · step through the algorithmlist = [1,2,6,3,4,5,6], val = 6
linked list with dummy
D0
11
22
63
34
45
56
67
prev
Prepend dummy node before head. Set prev = dummy (index 0). Target value to remove: 6.
Pseudocode
dummy.next = head
prev = dummy
while prev.next != null:
if prev.next.val == val:
prev.next = prev.next.next
else:
prev = prev.next
return dummy.next
Variables
prev
D
val
6
step 1 / 16
05Step-by-step dry run
1
linked list with dummy
D0
11
22
63
34
45
56
67
prev
Prepend dummy node before head. Set prev = dummy (index 0). Target value to remove: 6.
prev = Dval = 6
2
linked list with dummy
D0
11
22
63
34
45
56
67
prevnext
Peek ahead: prev.next has value 1. Compare with target 6.
prev.val = Dprev.next.val = 1val = 6
3
linked list (kept node)
D0
11
22
63
34
45
56
67
prev
Value 1 does not equal 6: keep node and advance prev = prev.next.
prev.val = 1
4
linked list with dummy
D0
11
22
63
34
45
56
67
prevnext
Peek ahead: prev.next has value 2. Compare with target 6.
prev.val = 1prev.next.val = 2val = 6
5
linked list (kept node)
D0
11
22
63
34
45
56
67
prev
Value 2 does not equal 6: keep node and advance prev = prev.next.
prev.val = 2
6
linked list with dummy
D0
11
22
63
34
45
56
67
prevnext
Peek ahead: prev.next has value 6. Compare with target 6.
prev.val = 2prev.next.val = 6val = 6
7
linked list (node unlinked)
D0
11
22
33
44
55
66
prev
Value 6 equals 6: unlink node with prev.next = prev.next.next. Notice prev does not advance.
prev.val = 2unlinked = 6
8
linked list with dummy
D0
11
22
33
44
55
66
prevnext
Peek ahead: prev.next has value 3. Compare with target 6.
prev.val = 2prev.next.val = 3val = 6
9
linked list (kept node)
D0
11
22
33
44
55
66
prev
Value 3 does not equal 6: keep node and advance prev = prev.next.
prev.val = 3
10
linked list with dummy
D0
11
22
33
44
55
66
prevnext
Peek ahead: prev.next has value 4. Compare with target 6.
prev.val = 3prev.next.val = 4val = 6
11
linked list (kept node)
D0
11
22
33
44
55
66
prev
Value 4 does not equal 6: keep node and advance prev = prev.next.
prev.val = 4
12
linked list with dummy
D0
11
22
33
44
55
66
prevnext
Peek ahead: prev.next has value 5. Compare with target 6.
prev.val = 4prev.next.val = 5val = 6
13
linked list (kept node)
D0
11
22
33
44
55
66
prev
Value 5 does not equal 6: keep node and advance prev = prev.next.
prev.val = 5
14
linked list with dummy
D0
11
22
33
44
55
66
prevnext
Peek ahead: prev.next has value 6. Compare with target 6.
prev.val = 5prev.next.val = 6val = 6
15
linked list (node unlinked)
D0
11
22
33
44
55
prev
Value 6 equals 6: unlink node with prev.next = prev.next.next. Notice prev does not advance.
prev.val = 5unlinked = 6
16
cleaned list
10
21
32
43
54
Traversal finished (prev.next == null). Return dummy.next resulting in list of length 5.
result = 1,2,3,4,5
06Java solution
RemoveLinkedListElements.java
public class RemoveLinkedListElements { static class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } public static ListNode removeElements(ListNode head, int val) { // Dummy node placed before head handles removing the head cleanly ListNode dummy = new ListNode(0, head); ListNode prev = dummy; while (prev.next != null) { if (prev.next.val == val) { // Bypass matching node; do NOT advance prev prev.next = prev.next.next; } else { // Keep node and advance prev = prev.next; } } return dummy.next; } public static void main(String[] args) { // [1, 2, 6, 3, 4, 5, 6], remove 6 ListNode head1 = new ListNode(1, new ListNode(2, new ListNode(6, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6))))))); ListNode res1 = removeElements(head1, 6); printList(res1); // 1 2 3 4 5 // [7, 7, 7, 7], remove 7 ListNode head2 = new ListNode(7, new ListNode(7, new ListNode(7, new ListNode(7)))); ListNode res2 = removeElements(head2, 7); printList(res2); // (empty line) } private static void printList(ListNode node) { ListNode curr = node; while (curr != null) { System.out.print(curr.val + (curr.next != null ? " " : "")); curr = curr.next; } System.out.println(); }}
Expected output
1 2 3 4 5
Tricky lines, decoded
new ListNode(0, head): Attaching dummy in front of head unifies all deletions into a single loop.
if (...) prev.next = prev.next.next; else prev = prev.next;: Do not advance prev after unlinking a node. If consecutive nodes have value val (such as [7, 7, 7]), the newly exposed prev.next must be checked on the immediate next iteration.
07Why it works
To delete node C located between B and D, B.next must be set to D. If C is the head, no predecessor B exists. Prepending a dummy node ensures that every real node has a valid predecessor pointer.
Because prev only advances when prev.next is verified to be kept, back-to-back target nodes are unlinked one after another without skipping any.
Invariant: At any step, all nodes from dummy up to prev have values strictly different from val.
08Time and space
Time: O(n). Each node is visited and checked exactly once.
Space: O(1). Only one sentinel node and a traversal pointer are allocated.
09Edge cases
Input
Output
Why it matters
[], val = 1
[]
Empty list handled without null errors
[7, 7, 7, 7], val = 7
[]
All nodes match; returns empty list
[6, 1], val = 6
[1]
Head matches; new head becomes next node
[1, 6], val = 6
[1]
Tail matches; unlinks last node
[1, 2, 3], val = 4
[1, 2, 3]
No nodes match target
10Common mistakes
Using a separate while loop to delete the head. While possible, it creates duplicate logic and easily fails on inputs like [7, 7, 7] when the head changes repeatedly.
Advancing prev after unlinking. Writing prev = prev.next inside the deletion branch skips the next node, failing on consecutive matches like [1, 2, 2, 1].
Returning head instead of dummy.next. If the original head was unlinked, head still points to the old discarded node. Always return dummy.next.
11Other approaches
Approach
Time
Space
Use when
Dummy + prev
O(n)
O(1)
Default interview solution
Head loop + body loop
O(n)
O(1)
If dummy nodes are forbidden
Recursion
O(n)
O(n)
Compact functional code, small lists
12In the interview
Recursive alternative:
java
public ListNode removeElements(ListNode head, int val) { if (head == null) return null; head.next = removeElements(head.next, val); return head.val == val ? head.next : head;}
Discuss why the iterative dummy node solution is preferred in production Java to avoid StackOverflowError on lists with thousands of elements.
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why must we NOT advance prev = prev.next when a matching node is unlinked?
2. Why is returning dummy.next preferred over returning head?
14Solve it yourself
Apply the same prev pointer technique to LeetCode 83 (Remove Duplicates from Sorted List):
Notice that since the list is sorted, duplicates are always adjacent: curr.val == curr.next.val.
Unlink duplicates using curr.next = curr.next.next without moving curr.