Delete Node in a Linked List (LeetCode 237): Solution and Explanation
Delete a given node from a singly linked list of unique values when you cannot reach the node before it. The node is never the tail. On Codemia the function receives head plus the value to delete, so the judge can build the list, and returns head.
Intuition
The usual way to delete a node is to make its predecessor skip it: prev.next = node.next. Here that is impossible, because LeetCode hands you only the node itself and a singly linked list has no pointer backwards.
What you can still change is this node and everything after it. So instead of removing this node, make it become its successor: copy node.next.val into it. Now the list holds the successor's value twice and the value you wanted gone has disappeared.
The duplicate is the successor node, and you can unlink that one, because you hold its predecessor: set node.next = node.next.next.
Seen from outside, the list now reads exactly as if the original node had been removed. The node that physically left is the successor, which is why the problem promises the target is never the tail: the tail has no successor to copy from.
Approach: Copy the next value, then skip the next node
- Codemia version only: walk
targetfromheaduntiltarget.val == node. On LeetCode you are handed this node directly. - Copy the successor's value:
target.val = target.next.val. - Unlink the successor:
target.next = target.next.next. - Return
head.
Step-by-step example
head = [4,5,1,9], node = 5
targetstarts at 4. 4 is not 5, so it moves to the node holding 5.- The node before it (4) is treated as out of reach, so its
nextcannot be changed. target.val = target.next.valwrites 1 into the target. The list reads 4, 1, 1, 9.target.next = target.next.nextskips the old 1 node. The list reads 4, 1, 9, which is the answer.
Code: Python, Java, C++
1class Solution:
2 def deleteNode(self, head: Optional[ListNode], node: int) -> Optional[ListNode]:
3 target = head
4 while target.val != node:
5 target = target.next
6 target.val = target.next.val
7 target.next = target.next.next
8 return head1class Solution {
2 public ListNode deleteNode(ListNode head, int node) {
3 ListNode target = head;
4 while (target.val != node) {
5 target = target.next;
6 }
7 target.val = target.next.val;
8 target.next = target.next.next;
9 return head;
10 }
11}1class Solution {
2public:
3 ListNode* deleteNode(ListNode* head, int node) {
4 ListNode* target = head;
5 while (target->val != node) {
6 target = target->next;
7 }
8 target->val = target->next->val;
9 target->next = target->next->next;
10 return head;
11 }
12};Time and space complexity
O(1) for the deletion
Copying one value and changing one pointer is constant work. In the Codemia version, locating the node first takes O(n).
O(1)
Only one pointer is used; no nodes are created.
Other approaches
Unlink through the predecessor (time O(n), space O(1))
With access to head you could walk with a prev pointer and set prev.next = target.next. That is the normal deletion, but it needs the predecessor, which the original problem deliberately withholds.
Shift every value left (time O(n), space O(1))
Copy each following value one step back, all the way to the end, then cut off the last node. It works without the predecessor too, but it rewrites every later node instead of just one.
Common mistakes
- Trying to find the previous node. In the original problem there is no head, so the only nodes you can touch are the given node and the ones after it.
- Skipping before copying. If you set
target.next = target.next.nextfirst, the successor's value is lost. - Expecting it to work on the tail. With no successor there is nothing to copy, which is why the problem guarantees the node is not the tail.
- Assuming the node object itself is removed. The object that leaves the list is the successor; anything else still holding a reference to the target node now sees the successor's value.
Frequently asked questions
Why does the Codemia version take head and a value?
LeetCode passes a reference to a node inside a list it built. Codemia's judge builds lists from arrays and cannot pass a node reference, so it passes head plus the value to find. After locating the node, the solution is the same copy-then-skip trick.
Is the node really deleted?
The value is deleted and the list is one node shorter, but the node object that leaves is the successor. For this problem the result is judged only by the values in the list, so that counts as deleting the node.
Why are the values unique?
Uniqueness makes the expected output well defined and, in the Codemia version, makes the value identify exactly one node.
Why is the problem rated Medium if the code is two lines?
The difficulty is the idea, not the code. Most people first look for the previous node and get stuck; the insight is to change which node leaves instead of how it leaves.