Singly Linked List
linked-list
Data Structures
Circular Linked List
An operating system rotating turns among four CPU-bound processes, a music player on shuffle-repeat, and the classic Josephus problem all share the same shape: a finite list of items consulted in cyclical order, where 'after the last' means 'back to the first'. A **Circular Linked List** encodes that shape directly, with the last node's `next` pointer wired to the head instead of `null`. This lesson covers the circular singly linked list and its doubly linked variant, the stop condition for traversal (return to the starting node rather than waiting for `null`), insertion at the head and at the tail in `O(1)` when a tail pointer is maintained, and deletion that correctly handles the wrap-around. You will also compare circular structures with the standard linear forms so you know when bending the list back on itself is the right call and when it just complicates traversal. In **Linked Lists (Singly)**, traversal terminated when you reached a `null` next pointer; here, the same loop must compare against the starting node instead, a small change that exposes the distinction between cycle detection and cycle definition (Floyd's tortoise-and-hare reuses this idea later). With all three linked-list shapes covered, the curriculum next pivots to tree-based and array-based structures with stronger time guarantees.
Not Started
0%
Doubly Linked List
Deleting a node from a singly linked list when you only hold a pointer to that node is genuinely awkward: you need the previous node too, and finding it is `O(n)`. Add a `prev` pointer to every node and the same delete becomes four pointer rewires in `O(1)`, which is exactly the upgrade that powers LRU caches and the browser back-forward stack. This lesson covers the **Doubly Linked List** node layout (`value`, `next`, `prev`), bidirectional traversal, insertion at head and tail and after a given node, deletion by node reference and by value, and the sentinel (dummy head and tail) pattern that eliminates almost all null-check edge cases in pointer-heavy code. You will also weigh the per-node memory cost of the extra pointer against the operations it unlocks. In **Linked Lists (Singly)**, head insertion was already `O(1)` but mid-list deletion required carrying a trailing pointer during traversal. The doubly linked list removes that constraint: every node knows its predecessor, so you can splice it out without any extra bookkeeping. Next, **Circular Linked List** keeps a single direction of links but bends the list back on itself, which turns out to be the right shape for round-robin scheduling and circular buffers.
Not Started
0%
Linked Lists (Singly)
Inserting at the front of an array of one million items forces every existing element to slide one slot to the right. A **Linked List** sidesteps that copy entirely: a head insertion is two pointer assignments and nothing else, no matter how many nodes follow. In this lesson, each node holds a value and a `next` reference, and you chain those nodes into a singly linked list with explicit head and optional tail pointers. You will implement traversal, head and tail and middle insertion, deletion by value and by position, and linear search, and you will analyze each operation precisely so the trade-off against arrays becomes concrete: `O(1)` insertion at the head, `O(n)` access by index, and per-node memory overhead for the pointer. In **Arrays & Strings**, the `O(n)` cost of inserting near the front came from shifting; the linked list was invented to remove that shift. **Memory Models** introduced the difference between a contiguous block on the stack or heap and scattered allocations connected by references, and that mental model is exactly how a node-based structure lives in memory. Once you are comfortable manipulating `next` pointers, **Stack (LIFO)** uses a linked list (or a dynamic array) as its underlying storage and adds a strict push, pop, peek discipline on top.
Not Started
0%
Skip List
Redis backs its sorted sets with one. LevelDB and RocksDB use one for their in-memory memtables. Java ships a `ConcurrentSkipListMap` in its standard library because lock-free skip lists are dramatically easier to implement than lock-free balanced BSTs. The structure those production systems share is a **Skip List**: a sorted linked list with a few extra express-lane pointers that turn `O(n)` search into `O(log n)` expected. This lesson covers the multi-level structure (a base list with every element, plus higher levels each containing a random subset that acts as a fast-path index), the coin-flip promotion rule that decides how many levels each new node spans, and search, insert, and delete operations whose expected cost is logarithmic without any tree rotations or balance bookkeeping. You will trace the search path that drops one level at a time whenever the next pointer overshoots the target, and you will analyze why a fair coin produces the right level distribution. In **Linked Lists (Singly)**, a search was `O(n)` because the list offered exactly one pointer per node. A skip list keeps that simple node, just adds a small array of forward pointers to higher levels, and lets randomness substitute for the deterministic balancing that AVL and Red-Black trees rely on. Next, **Balanced BST (AVL / Red-Black)** is the deterministic counterpart: same logarithmic guarantees, fundamentally different implementation strategy.
Not Started
0%
Algorithms
Linked List Algorithms
Reversing a singly linked list is a four-line iteration with three pointers (`prev`, `curr`, `next`), and yet roughly a third of candidates implement it incorrectly under interview pressure. The reason is that linked-list problems give you no random access: every operation has to be expressed as careful, ordered pointer rewrites, and one missed assignment loses the rest of the list forever. **Linked List Algorithms** is the lesson where pointer manipulation becomes a reliable skill. You will reverse a list iteratively and recursively, then extend the technique to reverse in groups of `k`. You will use Floyd's fast and slow pointers to detect cycles, find the cycle start, locate the middle element, and check whether a list is a palindrome. You will merge two sorted lists, then merge `k` sorted lists with a min-heap. The lesson also covers removing the nth node from the end, finding the intersection of two lists, deep-copying a list with random pointers, and the dummy-node trick that eliminates head-edge cases entirely. In **Linked Lists (Singly)**, you saw how nodes and `next` pointers form a list and why insertion at a known position is `O(1)`. **Two Pointers (Intro)** introduced fast and slow indices on arrays; this lesson reuses the same idea on pointer-linked nodes. Next, **Tree Algorithms** generalizes pointer-and-recursion thinking to branching structures.
Not Started
0%
Practice Problems
Linked List Cycle
Given the head of a linked list, determine if the linked list has a cycle in it.
Merge Two Sorted Lists
Merge two sorted linked lists into one sorted linked list by splicing the nodes together.
Middle of the Linked List
Given the head of a singly linked list, return the middle node. If there are two middle nodes, return the second one.
Reverse Linked List
Given the head of a singly linked list, reverse the list and return the new head.
Merge k Sorted Lists
Merge k sorted linked lists into one sorted linked list using a divide-and-conquer or min-heap approach.
Reverse Nodes in k-Group
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. Nodes left over at the end that are fewer than k should remain in their original order.
Add Two Numbers
Given two non-empty linked lists representing two non-negative integers in reverse order, add the two numbers and return the sum as a linked list.
Copy List with Random Pointer
Create a deep copy of a linked list where each node has a next pointer and a random pointer that can point to any node in the list or null.
Find the Duplicate Number
Given an array of n + 1 integers where each integer is in the range [1, n], find the one duplicate number without modifying the array and using only constant extra space.
LRU Cache
Design a data structure that follows the Least Recently Used (LRU) cache constraints, supporting get and put operations in O(1) time.
Partition List
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x, preserving the original relative order.
Remove Duplicates from Sorted List II
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Reorder List
Reorder a linked list from L0 -> L1 -> ... -> Ln to L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ... in-place.
Reverse Linked List II
Given the head of a singly linked list and two integers left and right, reverse the nodes of the list from position left to position right, and return the reversed list.
Rotate List
Given the head of a linked list, rotate the list to the right by k places.
Sort List
Given the head of a linked list, sort it in ascending order using O(n log n) time and O(1) or O(log n) space.
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You must solve it without modifying the values in the list's nodes.
Code Snippets
Singly Linked List Template
Linked lists rarely beat arrays in production JavaScript, but they are the single most-asked interview data structure and the conceptual foundation for skip lists, LRU caches, and queue implementations. This snippet covers the Node + List skeleton with O(1) prepend, an O(n) traversal helper, and the in-place reverse that shows up in nearly every interview round.
Doubly Linked List Template
A doubly linked list adds a `prev` pointer to every node, which unlocks O(1) removal of a known node and O(1) traversal in both directions. This is the structural backbone of LRU caches, browser back / forward stacks, and skip-list-adjacent designs. This snippet covers the Node + List skeleton with sentinel head and tail, the unlink-known-node operation that makes LRU possible, and a forward / backward iteration helper.
Question Banks
Linked List Basics Quiz
Short prompts on singly vs doubly linked lists, traversal, length, and the everyday operations that come up before cycle detection or in-place reversal.
Linked List Trace and Pointers
Four mid-level prompts on in-place reversal, node swapping, and the trickiest pointer bug. Mixes implementation and step-by-step trace.
Linked List Interview Prep
Five harder prompts on cycle detection, in-place reversal of a sublist, and merging two sorted lists. Code-anchored with one bug hunt.
Community
Design HashMap
Implement a HashMap from scratch using a fixed bucket array with separate chaining; support put / get / remove on integer keys.
Odd Even Linked List
Reorder a singly-linked list so all odd-positioned nodes come before even-positioned ones, in O(n) time and O(1) space using two interleaved sub-lists.
Intersection of Two Linked Lists
Find the node where two singly-linked lists merge using the two-pointer rendezvous trick that runs in O(m + n) time and O(1) space.
Design Linked List
Implement a singly-linked list class with index-based get / addAtHead / addAtTail / addAtIndex / deleteAtIndex, using a sentinel head and a size counter for O(1) bound checks.
Palindrome Linked List
Decide whether a singly-linked list reads the same forward and backward in O(n) time and O(1) space using fast/slow pointers plus an in-place reverse of the second half.
Remove Linked List Elements
Delete every node in a singly-linked list whose value matches a target, with a sentinel/dummy node that makes the head case fall out of the general loop.
Flatten a Multilevel Doubly Linked List
Flatten a doubly-linked list whose nodes may have a child pointer to another doubly-linked list, producing a single-level list via DFS or an explicit stack.
Linked Lists Explained
What linked lists actually buy you in modern code, the operations that make them shine, and why the answer is usually "use an array" anyway.
LFU Cache
Implement a Least Frequently Used (LFU) cache with O(1) get and put using a frequency-bucket map plus per-bucket doubly linked lists.
