Delete Node in a Linked List
Remove a node from a linked list when you are handed only that node and not the head.
A singly linked list holds some numbers. You get a reference to one of its nodes, nodenodenode
Example 1
Output: [4, 1, 9]
Example 2
Output: [4, 5, 9]
The Idea
A linked list stores its values in separate objects called nodes, such as the four nodes holding 4, 5, 1 and 9. Each node keeps one value and a link, named nextnextnull
How would you normally take 5 out of the list 4, 5, 1, 9? You would change the link of the node before it. The 4 would point straight at 1, and a walk from the head would no longer meet 5. Here, though, you are handed only the node holding 5. From it you can follow next
So how can a node leave the list when the link that leads to it cannot be changed? Think about what the list is to anyone who reads it: the values met on a walk from the head, in order. Nobody can tell which node object carries which value. So the node holding 5 does not have to leave. It can take over the value of the node after it, and then that next node is the one taken out. Its link comes from the node we hold, so we can change it. Look at the picture below.
In the row labelled start, nodenode.nextnodenode
The value 5 is gone, yet the node object that left the list is the one that held 1. This also shows why node
Solution Approach
Turning that idea into code means settling three things: why the node itself has to change, in which order its two fields change, and what goes wrong on the last node.
Changing the node, not a variable
Could the method simply move nodenode = node.nextnodenode.valnode.next
Copying first, then unlinking
Which field changes first? The value has to be copied while node.nextnode.nextnode.next.valnode.valnode.nextnode.next.next
Why the last node is ruled out
What if nodenextnullnode.next.valNullPointerExceptionnextnullnode
Java Solution
The method deleteNode
- First, copy the value of the next node into the given node with
. Both nodes now hold the same value.node.val = node.next.val - Then unlink the next node with
, sonode.next = node.next.next links straight to the node after it.node - The method returns nothing, because the list was changed in place, so any code holding the head sees the shorter list.
Next to the method, the class has a small ListNodebuildfindshow[4, 1, 9]main[7, 3]fiveone
public class DeleteNodeInLinkedList {
static class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
public static void deleteNode(ListNode node) {
node.val = node.next.val; // take over the next node's value
node.next = node.next.next; // and unlink the next node
}
static ListNode build(int... values) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
for (int v : values) {
tail.next = new ListNode(v);
tail = tail.next;
}
return dummy.next;
}
static ListNode find(ListNode head, int value) {
while (head.val != value) {
head = head.next;
}
return head;
}
static String show(ListNode node) {
StringBuilder sb = new StringBuilder("[");
for (; node != null; node = node.next) {
sb.append(node.val).append(node.next != null ? ", " : "");
}
return sb.append("]").toString();
}
public static void main(String[] args) {
ListNode head = build(4, 5, 1, 9);
deleteNode(find(head, 5));
System.out.println(show(head));
head = build(4, 5, 1, 9);
deleteNode(find(head, 1));
System.out.println(show(head));
head = build(7, 3);
deleteNode(head); // the first node of a two-node list
System.out.println(show(head));
head = build(4, 5, 1, 9);
ListNode five = find(head, 5);
ListNode one = find(head, 1);
deleteNode(five);
System.out.println(show(head) + " five.val = " + five.val
+ ", one.val = " + one.val + ", one.next.val = " + one.next.val);
}
}
Running it prints:
[4, 1, 9]
[4, 5, 9]
[3]
[4, 1, 9] five.val = 1, one.val = 1, one.next.val = 9
The second line, [4, 5, 9][3]headmain
Look at the last line. five.val = 1oneone.next.val = 9
Both wrong versions described earlier fail as expected when run on Example 1. A method whose body is only node = node.next[4, 5, 1, 9]deleteNode[4, 9, 9]
Complexity
The method does the same two assignments however long the list is.
- Time: O(1). It never walks the list or searches for the node before
. It reads one neighbour and rewrites two fields of the given node.node - Space: O(1). No new node is created. The removed node is simply no longer linked from the list.
When you have the head and a position to remove, see