Add linked-list module: singly and doubly lists with measured costs, reverse, middle, Floyd cycle detection, merge and 5 more interview problems, the 2015 menu programs and a doubly-list bug they exposed (15 tests, 15 transcripts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6r9iSyHu5qSJMzH2n
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
Deleting from a singly linked list: which one pointer changes
|
||||
=============================================================
|
||||
|
||||
remove the node at index 2 from 10 -> 20 -> 30 -> 40:
|
||||
before: 10 -> 20 -> 30 -> 40 -> null
|
||||
walk: before = node 20
|
||||
found: before = 20, doomed = 30, doomed.next = 40
|
||||
relink: 20.next = 40
|
||||
after: 10 -> 20 -> 40 -> null
|
||||
|
||||
removing the head has no node before it, so the head variable itself changes:
|
||||
before: head -> 10 -> 20 -> 30 -> null
|
||||
head = head.next
|
||||
after: head -> 20 -> 30 -> null
|
||||
|
||||
removing the tail needs the node before it, so it must be found by walking (and then tail moves back):
|
||||
list 10, 20, 30, 40, 50: removeLast() returned 50 after following 3 links; list is now [10, 20, 30, 40]
|
||||
Reference in New Issue
Block a user