↓Skip to main content
  1. LeetCode/

LeetCode 203: Remove Linked List Elements

·2 mins· ·
LeetCode Easy Linked-List Recursion
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-21

  • Submissions: Time Limit Exceeded → Runtime Error ×2 → Wrong Answer ×2 → Time Limit Exceeded → Wrong Answer → Runtime Error → Time Limit Exceeded ×2 → Accepted ×3

Approach
#

The head may equal val too, so put a dummy in front (Linked List Template 1). Then look only at cur.next: skip it when it must go and keep cur where it is (the new cur.next may need deleting too); advance only otherwise.

My Attempts
#

Getting this right in 2022 took 13 submissions, with three kinds of failure:

  • TLE: attempt 1 moved cur only inside the if, so it never advanced when nothing was deleted; [1], val = 3 looped forever.
  • RE / WA: it checked cur.val but deleted cur.next. When cur.next was None that raised; even when it did not, the head itself could never be deleted: [3], val = 3 returned [3]. Switching to checking cur.next.val then missed the head and the cur.next is None case.

Adding a dummy and inspecting only cur.next passed.

Solution
#

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        cur = dummy
        while cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return dummy.next

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The head may be deleted; use a dummy.
  • Do not advance cur after a deletion.

Interview Explanation
#

I add a dummy and only inspect cur.next: if it equals val I skip it and stay, otherwise I advance. Return dummy.next. Time O(n).

Related

LeetCode 206: Reverse Linked List
·1 min
LeetCode Easy Linked-List Recursion
LeetCode 206 note: three pointers prev / cur / nxt; save the next node, then point cur.next back at prev
LeetCode 141: Linked List Cycle
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 141 note: Floyd’s cycle detection, fast two steps and slow one; with a cycle they must meet, each advancing from its own position
LeetCode 160: Intersection of Two Linked Lists
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers
LeetCode 160 note: each pointer walks its own list then the other’s; both cover lenA + lenB steps and reach the intersection, or None, together
LeetCode 83: Remove Duplicates from Sorted List
·2 mins
LeetCode Easy Linked-List
LeetCode 83 note: sorted, so duplicates are adjacent; skip cur.next while it equals cur, and advance only otherwise
LeetCode 2: Add Two Numbers
·2 mins
LeetCode Medium Linked-List Math Recursion
LeetCode 2 note: digits are stored in reverse, so add from the head digit by digit with a carry, looping until both lists end and no carry remains
LeetCode 171: Excel Sheet Column Number
·1 min
LeetCode Easy Math String
LeetCode 171 note: scan left to right with res = res × 26 + letter value (A = 1)