Step 08 Linked-List — Fundamentals (Single LL)
Deletion in Linked List
Deleting a node from a singly linked list is pure pointer surgery: you never 'remove' memory directly, you just make the chain skip over the unwanted node, and the language's garbage collector reclaims it. This lesson walks through the four standard deletion cases — head, tail, position k, and a given value — because every harder deletion problem in this module is one of these cases in disguise.
The core idea: deletion is rewiring
To delete a node X, the node before X must point to the node after X. That single observation drives everything. It also explains the singly linked list's central awkwardness: given only a node, you cannot find its predecessor — you must walk from head keeping track of the previous node as you go.
Two things must be returned or preserved in every deletion routine: the (possibly new) head, and correctness on edge cases — empty list, single-node list, and deleting the first or last element. Handle those first in code, and the general case becomes short.
Case 1: deleting the head
The easiest case. If the list is empty, there is nothing to do — return None. Otherwise the new head is simply head.next; the old head becomes unreachable. This is O(1).
Forgetting to return the new head is the classic bug here: the deletion 'worked' inside the function, but the caller still points at the deleted node.
def delete_head(head):
if head is None:
return None
return head.next # old head is now unreferencedCase 2: deleting the tail
The tail has no successor, so deletion means making the second-last node the new tail: walk until temp.next.next is None — temp is then the second-last node — and set temp.next = None.
Edge cases: empty list returns None; a single-node list returns None too (deleting its tail deletes everything). The walk makes this O(n) time, O(1) space.
def delete_tail(head):
if head is None or head.next is None:
return None
temp = head
while temp.next.next is not None:
temp = temp.next
temp.next = None
return headCase 3: deleting the k-th node, and Case 4: deleting by value
For position k (1-indexed): if k == 1, it is the head case. Otherwise walk to the node just before position k, keeping a prev pointer, then splice: prev.next = prev.next.next. Guard against k being larger than the length — if you run off the end, leave the list unchanged.
Deleting by value X is the same shape: walk with prev and temp; when temp.data == X, splice prev.next = temp.next and stop. If X is in the head node, it degenerates to the head case again. Both are O(n) time and O(1) space.
def delete_kth(head, k):
if head is None:
return None
if k == 1:
return head.next
prev = None
temp = head
count = 1
while temp is not None and count < k:
prev = temp
temp = temp.next
count += 1
if temp is not None: # found position k
prev.next = temp.next
return head
def delete_value(head, x):
if head is None:
return None
if head.data == x:
return head.next
prev = head
while prev.next is not None and prev.next.data != x:
prev = prev.next
if prev.next is not None:
prev.next = prev.next.next
return headCommon mistakes
(1) Not returning the head — deletion of the first node changes what head is, so every delete function must return head and callers must use it. (2) Splicing with temp.next = temp.next.next without first confirming temp.next is not None. (3) In the by-value case, stopping the search at the tail without checking whether the tail itself holds X — the loop condition above checks prev.next, so the tail is examined too; get the condition shape right. (4) In C/C++, forgetting to free/delete the removed node leaks memory; in Python and Java the garbage collector handles it once the node is unreferenced.
Key points
- Deletion = make the predecessor point past the deleted node; the node itself is never modified.
- Deleting the head is O(1) and changes head — the function must return the new head.
- Tail, k-th and by-value deletions all walk with a prev pointer: O(n) time, O(1) space.
- Always handle empty list and single-node list before the general case.
- In a singly linked list you cannot delete a node given only that node (without its value trick) — you need the predecessor.