Home/Data Structures & Algorithms/Linked Lists & Custom Allocators

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Singly and doubly linked lists, XOR lists, cycle detection, memory pooling, and lock-free concurrent lists.
Floyd’s Tortoise and Hare: Mathematical proof of why meeting point resolves cycle origin
Floyd's cycle algorithm is pure mathematical poetry. Let's write out the distances with simple algebra so the proof is crystal clear. 1. Defining the Variables Let's map out the linked list into three distinct segments: L: Distance from the head to the cycle entrance. C: Total length (circumference)Read more
Floyd’s cycle algorithm is pure mathematical poetry. Let’s write out the distances with simple algebra so the proof is crystal clear.
1. Defining the Variables
Let’s map out the linked list into three distinct segments:
L: Distance from theheadto the cycle entrance.C: Total length (circumference) of the cycle.x: Distance from the cycle entrance to the meeting point inside the cycle.2. Distance Traveled by Each Pointer
When the Tortoise (slow) and Hare (fast) meet:
Dist_slow = L + xDist_fast = L + n * C + x(wherenis how many full laps fast ran around the cycle).Because the fast pointer moves at twice the speed of slow:
Now subtract
L + xfrom both sides:3. What does L = (n – 1) * C + (C – x) mean?
Look carefully at that equation:
Lis the distance from head to the cycle entrance.(C - x)is the distance from the meeting point to the cycle entrance!(n - 1) * Cis just zero or more full loops around the cycle!Conclusion: If you place Pointer 1 at
head(which must travel distanceL) and Pointer 2 atmeeting_point(which travels distance(C - x)plus some optional full laps), both pointers will meet at the EXACT same node: the cycle entrance!Clean Python 3.12 Implementation
Complexity Breakdown
- Time Complexity:
- Space Complexity:
See lessO(N). Phase 1 takes at most $2N$ steps. Phase 2 takes at most $N$ steps.O(1). No hash set or memory allocation.How to design a thread-safe LRU Cache in O(1) without memory leaks?
Building an LRU cache from scratch is one of the best ways to understand how data structures combine in real systems. The industry standard pattern is combining two complementary structures: A Hash Map: Gives you O(1) key-to-node lookups. A Doubly Linked List (DLL) with Dummy Head & Tail: GivesRead more
Building an LRU cache from scratch is one of the best ways to understand how data structures combine in real systems. The industry standard pattern is combining two complementary structures:
O(1)key-to-node lookups.O(1)node insertion at the front (most recent) andO(1)node removal from the back (least recent).The secret trick that eliminates 90% of bug-prone null checks is using sentinel (dummy) head and tail nodes. Instead of constantly checking
if (head == null)orif (node->prev == null), the dummy head and tail are always linked together:head <-> tail. Any real data node always lives safely in between them!Architectural Diagram
Clean, Idiomatic Python 3.12 Implementation
Why Storing the Key Inside the Node is Crucial
Notice that the
Nodeclass stores bothkeyandval. Many developers forget to storekeyin the node and only storeval. But when the cache reaches full capacity and you evicttail.prev, how do you delete that entry from the hash map? Withoutnode.key, you’d have to search the entire hash map inO(N)time, destroying yourO(1)guarantee!Thread Safety in Production
If multiple threads access this cache concurrently:
- In Python, use
- In Go or C++, a Read-Write Lock (
See lessthreading.Lock()aroundgetandput.sync.RWMutex/std::shared_mutex) is often tempting, but remember: even aget()operation mutates the linked list (to move the accessed item to the front)! Therefore, standard read locks are not enough—you must acquire an exclusive lock or use lock striping across multiple shards.