mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
1007 B
1007 B
Linked List
- unlike an array, a list does not provide constant time access to an index (as it needs to interact through all k elements), however addition and removal of elements are constant time.
- to remove a node you set
prev.next
equal tonode.next
. if it's a double list, you also updatenode.next
withnode.next.prev
tonode.prev
(and deallocate the memory).
detecting cycles
- can be done with hash table or the two-pointer technique, where if there is no cycle, the faster pointer (going 2 steps) will stop at the end of the list, but if there is a cycle, the fast pointer will eventually meet the slow pointer (going 1 step). if there is no cycle, the faster pointer takes
N/2
to reach the end of the list (N
being the length).
LinkedListFIFO.py
python LinkedListFIFO.py
Linked List FIFO
Add 1: None
Add 2: Nonew
Add 3: None
Length: 3
Find 1: (None, None, 0)
Delete 1: None
Length: 0
Find 1: (None, None, 0)