Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6r9iSyHu5qSJMzH2n
29 lines
1.0 KiB
Plaintext
29 lines
1.0 KiB
Plaintext
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)
|