LeetCode 203: Remove Linked List Elements – Step-by-Step Guide for Beginners
LeetCode 203: Remove Linked List Elements – Step-by-Step Guide for Beginners

LeetCode 203: Remove Linked List Elements – Step-by-Step Guide for Beginners

Learn How to Remove Nodes from a Linked List Using Iteration, Edge Case Handling, and Dummy Node Technique (With Clear Intuition)

7 views
0
0

Try the Problem

You can practice the problem here:

https://leetcode.com/problems/remove-linked-list-elements/


Problem Description (In Very Simple Words)

You are given:

  1. The head of a linked list
  2. An integer value val

Your task is:

👉 Remove all nodes from the linked list whose value is equal to val.

👉 Return the updated head of the linked list.


What is a Linked List? (Quick Recap)

A linked list is a chain of nodes where each node contains:

value + pointer to next node

Example:

1 → 2 → 6 → 3 → 4 → 5 → 6 → null


Example Walkthrough

Example 1

Input:

head = [1,2,6,3,4,5,6]
val = 6

Output:

[1,2,3,4,5]

Explanation

We remove all nodes with value 6.

1 → 2 → ❌6 → 3 → 4 → 5 → ❌6

Final list:

1 → 2 → 3 → 4 → 5

Example 2

Input:

head = []
val = 1

Output:

[]

Example 3

Input:

head = [7,7,7,7]
val = 7

Output:

[]

All nodes are removed.


Constraints

0 <= number of nodes <= 10^4
1 <= Node.val <= 50
0 <= val <= 50


Understanding the Problem Deeply

The tricky part is:

👉 Nodes can appear anywhere:

  1. At the beginning
  2. In the middle
  3. At the end
  4. Or all nodes

👉 Linked list does NOT allow backward traversal

So we must carefully handle pointers.


Thinking About the Solution

When solving this problem, multiple approaches may come to mind:

Possible Approaches

  1. Traverse the list and remove nodes manually.
  2. Handle head separately, then process the rest.
  3. Use a dummy node to simplify logic.
  4. Use recursion (less preferred for beginners).


Approach 1: Without Dummy Node (Manual Handling)

Idea

  1. First, remove nodes from the start (head) if they match val.
  2. Then traverse the list using two pointers:
  3. prev → previous node
  4. curr → current node

Key Challenge

Handling head separately makes code more complex.

Code

class Solution {
public ListNode removeElements(ListNode head, int val) {

// Step 1: Remove matching nodes from the beginning
while(head != null && head.val == val){
head = head.next;
}

// If list becomes empty
if(head == null) return null;

ListNode prev = head;
ListNode curr = head.next;

while(curr != null){

if(curr.val == val){
// Skip the node
prev.next = curr.next;
} else {
// Move prev only when node is not removed
prev = curr;
}

curr = curr.next;
}

return head;
}
}

Why This Works

  1. We ensure prev always points to the last valid node
  2. When we find a node to delete:
prev.next = curr.next
  1. This skips the unwanted node

Problem With This Approach

👉 Too many edge cases:

  1. Removing head nodes
  2. Empty list
  3. All nodes equal


Approach 2: Dummy Node (Best & Clean Approach)

Idea

We create a fake node (dummy) before the head.

dummy → head → rest of list

This helps us:

👉 Treat head like a normal node

👉 Avoid special cases

Visualization

Original:

1 → 2 → 6 → 3

After dummy:

-1 → 1 → 2 → 6 → 3


Java Implementation (Best Approach)

class Solution {
public ListNode removeElements(ListNode head, int val) {

// Step 1: Create dummy node
ListNode dumm = new ListNode(-1, head);

// Step 2: Start from dummy
ListNode curr = dumm;

while(curr.next != null){

// If next node needs to be removed
if(curr.next.val == val){

// Skip that node
curr.next = curr.next.next;

} else {

// Move forward only when no deletion
curr = curr.next;
}
}

// Step 3: Return new head
return dumm.next;
}
}


Step-by-Step Dry Run

Input:

1 → 2 → 6 → 3 → 4 → 5 → 6
val = 6

After dummy:

-1 → 1 → 2 → 6 → 3 → 4 → 5 → 6

Traversal:

curr = -1 → 1 (keep)
curr = 1 → 2 (keep)
curr = 2 → 6 (remove)
curr = 2 → 3 (keep)
curr = 3 → 4 (keep)
curr = 4 → 5 (keep)
curr = 5 → 6 (remove)

Final:

1 → 2 → 3 → 4 → 5


Time Complexity

O(n)

We traverse the list once.

Space Complexity

O(1)

No extra space used.


Why Dummy Node is Preferred

Without DummyWith Dummy
Complex edge casesClean logic
Special handling for headNo special handling
Error-proneSafe and readable


Approach 3: Recursive Solution (Conceptual)

Idea

We process one node at a time and recursively solve for the rest.

Code

class Solution {
public ListNode removeElements(ListNode head, int val) {

if(head == null) return null;

head.next = removeElements(head.next, val);

if(head.val == val){
return head.next;
} else {
return head;
}
}
}

Time Complexity

O(n)

Space Complexity

O(n)

(due to recursion stack)


Key Takeaways

  1. Linked list problems are all about pointer manipulation
  2. Always think about edge cases (especially head)
  3. Dummy node simplifies almost every linked list problem


Conclusion

The Remove Linked List Elements problem is perfect for understanding how linked lists work in real scenarios.

While the manual approach works, the dummy node technique provides a much cleaner and safer solution.

If you master this pattern, you will be able to solve many linked list problems easily in interviews.

👉 Tip: Whenever you feel stuck in linked list problems, try adding a dummy node — it often simplifies everything!

Ai Assistant Kas