Skip to content
Veltoryx Technologies
FreeProblem 45● EasyLeetCode #203 ↗5 min readUpdated 30 October 2026

Remove Linked List Elements

Remove all nodes with a given target value from a singly linked list in a single pass using a sentinel dummy head.

01The problem

Given the head of a singly linked list and an integer val, remove all the nodes of the linked list that have Node.val == val, and return the new head.

The head node itself might match val, possibly several times consecutively (for example [7, 7, 7, 7]).

InputOutputWhy
nums = [1, 2, 6, 3, 4, 5, 6], val = 6[1, 2, 3, 4, 5]Both occurrences of 6 are removed
nums = [7, 7, 7, 7], val = 7[]All nodes match target; returns an empty list
nums = [], val = 1[]An empty list has no elements to remove
nums = [6, 1], val = 6[1]Head matches and is removed; new head is 1

Constraints: 0 to 10,000 nodes, node values between 1 and 50, val between 1 and 50.

02How to spot it

  • You need to delete nodes matching a condition across an entire linked list.
  • The head node itself might be removed, which makes maintaining a simple head variable messy.
  • Deleting any node requires updating the next pointer of the node preceding it (prev.next = prev.next.next).
  • Introducing a fake sentinel node before the head allows you to treat every node—including the real head—with identical logic.

So: sentinel dummy node + single pass.

03Pseudocode

text
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
  1. Create a dummy node pointing to head.
  2. Walk a prev pointer, peeking ahead at prev.next.
  3. 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.
  4. Otherwise, advance prev = prev.next.
  5. 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

  1. dummy.next = head
  2. prev = dummy
  3. while prev.next != null:
  4. if prev.next.val == val:
  5. prev.next = prev.next.next
  6. else:
  7. prev = prev.next
  8. return dummy.next

Variables

prev
D
val
6
step 1 / 16

05Step-by-step dry run

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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

InputOutputWhy 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

ApproachTimeSpaceUse when
Dummy + prevO(n)O(1)Default interview solution
Head loop + body loopO(n)O(1)If dummy nodes are forbidden
RecursionO(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. 1. Why must we NOT advance prev = prev.next when a matching node is unlinked?

  2. 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):

  1. Notice that since the list is sorted, duplicates are always adjacent: curr.val == curr.next.val.
  2. Unlink duplicates using curr.next = curr.next.next without moving curr.