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,28 @@
|
||||
DoublyLinkedList: two links per node, and what that buys
|
||||
========================================================
|
||||
|
||||
addLast(20), addFirst(40), addLast(60)
|
||||
forward (next links): [40, 20, 60]
|
||||
backward (prev links): [60, 20, 40]
|
||||
|
||||
addAfter(node 20, 45) and addBefore(node 20, 65)
|
||||
forward: [40, 65, 20, 45, 60]
|
||||
backward: [60, 45, 20, 65, 40]
|
||||
|
||||
addAfter(the last node, 99): the tail has to move, and it does
|
||||
forward: [40, 65, 20, 45, 60, 99]
|
||||
backward: [99, 60, 45, 20, 65, 40]
|
||||
last().val = 99, removeLast() returned 99
|
||||
|
||||
Links followed to reach one element of a 1000-element list:
|
||||
index | doubly (nearer end) | singly (from head)
|
||||
0 | 0 | 0
|
||||
250 | 250 | 250
|
||||
499 | 499 | 499
|
||||
500 | 499 | 500
|
||||
750 | 249 | 750
|
||||
999 | 0 | 999
|
||||
|
||||
Unlinking the element at index 700 when you already hold the node:
|
||||
doubly: 0 links followed (the node knows its neighbours)
|
||||
singly: 699 links followed (it must walk to the node before it)
|
||||
Reference in New Issue
Block a user