LeetCode #237 Medium

Delete Node in a Linked List

Delete Node in a Linked List is LeetCode 237 (Medium). You get one node of a singly linked list, and you must delete it. You are not given the head of the list.

  • The node is never the last one, and all values are distinct.
  • "Deleted" means the list reads the same as before with this node's value gone: one node fewer, and every other value in its old order.
  • The function changes the list in place and returns nothing.
Constraints
  • The number of the nodes in the given list is in the range [2, 1000].
  • -1000 <= Node.val <= 1000
  • The value of each node in the list is unique.
  • The node to be deleted is in the list and is not a tail node.
linked-list
Open on LeetCode ↗
02

Intuition

Normal singly linked list deletion points the previous node past the target. Here the previous node cannot be reached: links only go forward, and there is no head to walk from.

So change what gets removed. Copy the next node's value into the given node, then unlink the next node. The given node now plays the part of its successor, and the successor's old box drops out of the chain. Anyone reading the list sees every value except the deleted one, in the same order.

This only works because the node is never the tail: a tail has no next node to copy from.

How to spot this pattern

Any request to delete a node without the head pointer has this shape: you hold a node but not the node before it, so you cannot rewire the link into it. Ask whether moving data instead of links gives the same visible result.

03

Approach

Try it first

Before reading on, list which pointers you can actually reach from the given node, and which one normal deletion needs. Then decide what you could change instead so the list reads as if the node were gone. Aim for O(1) time and O(1) space.

1

Copy the next value into this node

Set node.val = node.next.val. The value to delete is overwritten, and for a moment the next value appears twice in a row. The next node exists because the given node is never the tail.

2

Unlink the next node

Set node.next = node.next.next. The duplicate box is skipped, so the list is one node shorter and reads exactly as if the original node had been removed. If the next node was the tail, node.next becomes null and the given node is the new tail.

3

Free the skipped node in C++

Save node->next before relinking and delete it afterwards. Nothing points to it any more, so Python and Java reclaim it on their own, but C++ would leak it.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def deleteNode(self, node):
▶3 node.val = node.next.val
▶4 node.next = node.next.next
null4out of reach5node19you hold node 5 only; 4 is out of reach
node.val5
node.next.val1
Normal deletion would point 4 past 5. But links only go forward and the head is not given, so nothing before the given node can be reached.
null4out of reach1node · was 51copy9copy 1 into this node → 1 shows twice
node.val1was 5
The value 5 is overwritten, so it is gone from the list. For now the list reads 4 → 1 → 1 → 9: the next node is a leftover copy.
null4out of reach1node1skipped9point past the copy → it drops out
node.next9
The given node now links straight to 9. No arrow leads into the old copy any more, so walking the list never visits it.
null419the list reads 4 → 1 → 9
list4 → 1 → 9
Every value except 5 is still there, in its old order, and the list is one node shorter. That is all the problem checks. The box that was removed held the next value, not 5, but no reader of the list can tell.
05

Common pitfalls

Reassigning the parameter

✗ Wrong
node = node.next
✓ Right
node.val = node.next.val
node.next = node.next.next

This only changes the local name node. The list itself is untouched, so [4,5,1,9] with node 5 still reads 4 5 1 9. Only changes to the node's own fields reach the list.

Copying the value but not unlinking

✗ Wrong
node.val = node.next.val
✓ Right
node.val = node.next.val
node.next = node.next.next

For [4,5,1,9] with node 5, the list becomes 4 1 1 9: the value is gone but the next value now appears twice and the length is unchanged.

Unlinking before copying

✗ Wrong
node.next = node.next.next
node.val = node.next.val
✓ Right
node.val = node.next.val
node.next = node.next.next

After the first line node.next already points two nodes ahead, so the value copied is the wrong one. For [4,5,1,9] with node 5 you get 4 9 9.

06

Edge cases

The given node is second to last

node.next.next is null, so after the copy the given node points to nothing and becomes the new tail. For [4,5,1,9] with node 1, the list ends 4 5 9.

Some other code still holds the next node

That reference now points to a box outside the list (and in C++ to freed memory). The trick is only safe when nothing outside the list keeps pointers to its nodes, which the problem guarantees.

07

Complexity

Time
O(1)
Space
O(1)
Two field writes, whatever the list length. No traversal is needed because nothing before the node is ever touched.
08

Delete Node in a Linked List FAQ

Can this delete the last node?

No. The tail has no next node to copy, and without the head there is no way to reach the node before it to cut the link. That is why the problem promises the given node is never the tail. With the head you would walk to the second-to-last node and set its next to null.

How is this different from removing a value when the head is given?

With the head you can walk with a prev pointer and relink around any node, including the head and the tail; see Remove Linked List Elements and Delete the Middle Node of a Linked List. Here the copy trick replaces that walk, so the cost drops to O(1) but the tail case is lost.