Remove Linked List Elements
Remove Linked List Elements is LeetCode 203 (Easy). You get the head of a linked list and an integer val. Remove every node whose value equals val and return the new head.
- The list is not sorted, so matching nodes can be anywhere, including the head and several in a row.
- The list can be empty, and every node can match, which leaves an empty list.
- The number of nodes in the list is in the range [0, 10⁴].
- 1 <= Node.val <= 50
- 0 <= val <= 50
Intuition
A singly linked node cannot unlink itself, because the link that has to change belongs to the node before it. So the walk stands on a node and judges the one after it.
- Judge the next node: if
prev.nextmatches, pointprevpast it; otherwise moveprevonto it. - Placeholder in front: the head has no node before it. A dummy node supplies one, so a matching head is removed by exactly the same line as any other node.
Deleting nodes by a test on their value, with the head possibly deleted, is the signal for a dummy node plus a pointer that always looks one node ahead.
Approach
Before reading on, decide which node you stand on when you delete a node, and what you stand on when the head itself must go. Then trace [7, 7, 7] with val = 7. Aim for O(n) time and O(1) extra space.
Add a node in front of the head
Create dummy whose next is head, and set prev = dummy. Now every real node, the head included, has a node before it whose link can be changed.
Look at the node after prev
Loop while prev.next exists. The candidate for deletion is always prev.next, never prev, because prev is a node that has already been accepted.
Unlink a match, or accept a keeper
If prev.next.val == val, set prev.next = prev.next.next and leave prev where it is, since the node that slid into place is still unjudged. Otherwise move prev forward onto the node it just accepted.
Return what follows the placeholder
Return dummy.next. If the old head was deleted this is a later node, and if every node matched it is null, so the placeholder never leaks into the answer.
Code in Python, C++ and Java
Common pitfalls
Stepping prev forward after a removal
if prev.next.val == val:
prev.next = prev.next.next
prev = prev.nextif prev.next.val == val:
prev.next = prev.next.next
else:
prev = prev.nextWith [1, 6, 6, 2] and val = 6, removing the first 6 brings the second 6 next to 1, and the unconditional step jumps onto it. It is never judged, so the result is [1, 6, 2].
Returning head instead of dummy.next
return head
return dummy.next
For [7, 7] with val = 7, head is still the first 7: it was unlinked from the dummy, not changed itself. Its own link to the second 7 never changed, so returning it hands back [7, 7] instead of an empty list.
Edge cases
prev never leaves the dummy. Each pass removes the new first node, and dummy.next ends as null.
Node values are at least 1, so val = 0 matches nothing. The walk only steps forward and returns the list unchanged.
Complexity
prev past one node, so there are at most n passes. Up to 10⁴ nodes is small for one walk.Remove Linked List Elements FAQ
Is there a recursive solution?
Yes: clean the rest of the list first with head.next = removeElements(head.next, val), then return head.next if head matches, else head. It is three lines but keeps one stack frame per node, up to 10⁴ deep.
Can I do it without a dummy node?
Yes. First move head forward while it matches, then run the same look-ahead loop from the new head. It works, but the head case is written twice, which is the duplication the dummy avoids.
How is this different from deleting a node when only that node is given?
In Delete Node in a Linked List you have no access to the node before, so you copy the next value over and delete the next node instead. Here you have the head, so you can always reach the node before.