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.
- 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.
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.
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.
Approach
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.
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.
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.
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.
Code in Python, C++ and Java
node.next.next is null. The given node now ends the list.node.next.next is null. The given node now ends the list.Common pitfalls
Reassigning the parameter
node = node.next
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
node.val = node.next.val
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
node.next = node.next.next node.val = node.next.val
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.
Edge cases
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.
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.
Complexity
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.