
The key takeaways
- Linked list interview questions test pointer precision over theory; one misplaced assignment either loses the rest of the list or creates an infinite loop
- In-place reversal uses three pointers (prev, curr, next) in O(n) time and O(1) space; the recursive version costs O(n) call stack and interviewers will ask the difference
- Dummy head nodes eliminate boundary special cases for head insertion and deletion; use one habitually and your code gets shorter and cleaner under pressure
- Floyd's cycle detection (tortoise and hare) finds cycles in O(n) time, O(1) space; fast gains exactly one step on slow per iteration, so meeting is guaranteed
- Cycle entry point: after detection, reset one pointer to head and walk both one step at a time; they converge at the cycle start because F = kC - a
- Fast/slow pointer solves middle-finding, kth-from-end, and palindrome checks with O(1) extra space by combining patterns in a single pass
- Cache misses are the hidden cost: scattered heap nodes mean up to n cache misses per traversal, which is why arrays beat linked lists in practice despite equal big-O
Ask any CS student to describe a linked list and they'll nail it in fifteen seconds. Nodes, pointers, null at the end. Dead simple. Then ask them to reverse one in a live interview, with a clock running, and suddenly that confidence evaporates somewhere around step two.
Linked lists are not hard. They reward mechanical precision. One misplaced pointer assignment and you've either lost the rest of your list or created an infinite loop. Interviewers use them to watch how you think under pressure: do you track mutable state carefully? Can you prove your approach is correct instead of just hoping it works?
This post goes deep. Memory layout, pointer mechanics, the iterative reversal trick, Floyd's cycle detection with the full mathematical proof, and the pattern recognition that lets you spot a linked list problem from the first sentence of the prompt.
Every node needs a predecessor. Every single one.
What It Is
A singly linked list is a sequence of nodes where each node holds a value and a pointer to the next. The list itself is just a reference to the head node. The last node's next pointer is null (or None, or nil, depending on your language).
Mental model: a treasure hunt where each clue tells you where the next one is, but you can only ever move forward. Lose any clue and the rest of the treasure is gone forever.
You reach for a linked list when you need cheap prepend, cheap delete-at-known-position, and you can live without random access.
How It Works Internally
Node Structure
Every node is two fields. That is it.
┌─────────────┬───────────┐
│ value │ next │
│ (4 bytes) │ (8 bytes) │
└─────────────┴───────────┘
Twelve bytes of actual data, sixteen with alignment padding. You're paying pointer tax on every single element.
On a 64-bit system, a pointer is 8 bytes. An int is typically 4 bytes. So a node holding a single integer carries 12 bytes of data but occupies at least 16 bytes due to alignment padding. The pointer overhead is real: for small values, the pointer can double or triple the per-element memory cost compared to a plain array.
Memory Layout
This is the part textbooks gloss over. And honestly, it's the part that explains why linked lists have a reputation that doesn't match their Big-O.
head
│
▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ val: 1 │ │ val: 2 │ │ val: 3 │
│ next: ─────┼────►│ next: ─────┼────►│ next: null │
└────────────┘ └────────────┘ └────────────┘
0x7f3c2a10 0x7f3c8b40 0x7f3c1d90
Nodes scattered across the heap. Your CPU has to fetch each one from a potentially different cache line.
Notice the addresses. They are not sequential. Each node is heap-allocated independently, so nodes end up scattered across memory. When you traverse the list, the CPU fetches each node from a potentially different cache line. Traversing a linked list of n elements can trigger n separate cache misses, while an array traversal hits one cache line every ~16 integers.
Cache misses cost around 200 clock cycles each. That adds up embarrassingly fast. This is why, in practice, a Vec or slice often beats a linked list even for workloads where linked lists have theoretically better Big-O complexity. Your Big-O doesn't account for the hardware weeping quietly in the background.
The Head Pointer
The list is nothing but a reference to the first node. Lose that reference and you cannot reach any node. The garbage collector (or you, in C) has to deal with the orphaned memory.
A common optimization is to maintain a tail pointer alongside head. Without it, appending to the end requires traversing the entire list (O(n)). With it, append becomes O(1). Many interview problems give you just head to force you to think about traversal.
The Sentinel (Dummy Head) Trick
Before writing any linked list code in an interview, consider this: add a dummy node before the real head.
dummy → node1 → node2 → node3 → nullThe dummy node holds no meaningful data. Its only job is to make sure every real node has a predecessor. This kills the special case for head deletion and head insertion. Code that handles position i in the middle now handles position 0 identically. Fewer if head is None checks, fewer off-by-one bugs, cleaner code under pressure.
The dummy head is functionally useless and structurally invaluable. Kind of like that one comment everyone leaves in the code that turns out to be load-bearing.
Core Operations
| Operation | Time | Space | Notes |
|---|---|---|---|
| Prepend (insert at head) | O(1) | O(1) | Just update head pointer |
| Append (insert at tail, no tail ptr) | O(n) | O(1) | Must traverse to end |
| Append (insert at tail, with tail ptr) | O(1) | O(1) | Update tail.next and tail |
| Delete at head | O(1) | O(1) | head = head.next |
| Delete by value | O(n) | O(1) | Traverse to find predecessor |
| Search / access by index | O(n) | O(1) | No random access |
| Length (uncached) | O(n) | O(1) | Must count all nodes |
| Reverse (in-place) | O(n) | O(1) | Three-pointer walk |
| Cycle detection (Floyd's) | O(n) | O(1) | Two-pointer walk |
Why Each Bound Holds
Prepend is O(1) because you allocate one node, point its next to the current head, then update head. No traversal, no loop.
Delete at head is O(1) for the same reason in reverse: save head.next, release the old head node, update head. Done.
Delete by value is O(n) because you need to find the node whose next points to the target. In a singly linked list you cannot reach a node's predecessor from the node itself (no back-pointer), so you walk from the head. Once you have the predecessor in hand, the actual deletion is O(1): predecessor.next = predecessor.next.next.
Search is O(n) in the worst case because there is no index, no hash, and the data is not necessarily sorted. You scan from head to tail.
Reverse is O(n) time, O(1) space using the iterative three-pointer approach. The recursive approach is also O(n) time but uses O(n) space on the call stack, one frame per node. This distinction matters and interviewers will ask.
In-Place Reversal
This is the operation that shows up most often in interviews. The naive instinct is to collect all values into an array and write them back. That works, but it uses O(n) space. Interviewers know this trick. They will ask for O(1). The optimal approach needs only three pointers.
prev curr next
null → [1] → [2] → [3] → nullAt each step:
- Save
curr.nextasnext_node - Flip
curr.nextto point atprev - Advance
prevtocurr - Advance
currtonext_node
Step by step through a three-node list:
Step 0: prev=null, curr=1, next=?
null ← [1] [2] → [3] → null
Step 1: prev=1, curr=2, next=?
null ← [1] ← [2] [3] → null
Step 2: prev=2, curr=3, next=?
null ← [1] ← [2] ← [3]
Step 3: curr=null, return prev (which is 3)
3 → 2 → 1 → null
Orange = prev, blue = curr. Each step flips one arrow. Don't blink.
The key insight: you must save curr.next before you overwrite it. Miss that one line and you lose the rest of the list permanently, into the void, unreachable and unrecoverable. This is exactly the trap interviewers are watching for.
def reverse(head):
prev = None
curr = head
while curr:
next_node = curr.next # save before overwriting
curr.next = prev # flip the pointer
prev = curr # advance prev
curr = next_node # advance curr
return prev # new headFast and Slow Pointers
Once you have two pointers walking a linked list at different speeds, you can solve a surprising range of problems with O(1) extra space.
Finding the Middle
Move slow one step at a time, fast two steps. When fast reaches the end, slow is at the middle.
[1] → [2] → [3] → [4] → [5] → null
Start: slow=1, fast=1
Step 1: slow=2, fast=3
Step 2: slow=3, fast=5 ← fast.next is null, stop
Middle = node 3For even-length lists, this gives you the "upper middle" (the second of the two center nodes). If you need the lower middle, adjust the termination condition to fast.next is not None and fast.next.next is not None.
Cycle Detection (Floyd's Algorithm)
The tortoise and hare. Move slow one step, fast two steps. If there is a cycle, they will meet inside it. If fast reaches null, there is no cycle.
[1] → [2] → [3] → [4] → [5]
▲ │
└─────────┘
The hare is faster but stuck going in circles. Eventually the tortoise catches up. The math here is genuinely satisfying.
Why they must meet: once both pointers are inside the cycle, the gap between them shrinks by exactly one step per iteration. Fast moves 2, slow moves 1, relative gain = 1 per round. That gap eventually hits zero modulo the cycle length. They land on the same node. Every time.
Finding the Cycle Entry Point
This is the harder variant and the one where most people stare at the board blankly for thirty seconds. Once you detect a cycle (slow and fast have met), reset one pointer to head and leave the other at the meeting point. Move both one step at a time. Where they meet again is the cycle's start.
The fact that this works feels like magic until you work through the proof. Then it still feels like magic, but you can explain it.
Why this works. Define:
F= steps from head to cycle starta= steps from cycle start to the meeting point (inside the cycle)C= cycle length
When slow and fast meet:
- slow has traveled:
F + a - fast has traveled:
F + a + k*C(fast lapped the cycle k extra times) - fast travels 2x slow:
F + a + k*C = 2(F + a) - Solving:
k*C = F + a, thereforeF = k*C - a
After the reset, pointer1 starts at head and needs F steps to reach the cycle start. Pointer2 is at the meeting point, a steps into the cycle. From the meeting point, k*C - a more steps forward around the cycle lands exactly at the cycle start (it's k full loops minus a steps of backtracking, which is the same as moving forward to the start). So both pointers arrive at the cycle start simultaneously after F steps.
Cycle entry finding:
Reset p1 to head. p2 stays at meeting point.
Move both 1 step at a time.
They meet at the cycle start.
The proof is three lines of algebra. Walk through it once and you will never forget it.
Implementations
from __future__ import annotations
from typing import Optional
class Node:
def __init__(self, value: int):
self.value = value
self.next: Optional[Node] = None
class SinglyLinkedList:
def __init__(self):
self.head: Optional[Node] = None
def prepend(self, value: int) -> None:
node = Node(value)
node.next = self.head
self.head = node
def append(self, value: int) -> None:
node = Node(value)
if not self.head:
self.head = node
return
current = self.head
while current.next:
current = current.next
current.next = node
def delete(self, value: int) -> bool:
dummy = Node(0)
dummy.next = self.head
prev = dummy
while prev.next:
if prev.next.value == value:
prev.next = prev.next.next
self.head = dummy.next
return True
prev = prev.next
return False
def search(self, value: int) -> Optional[Node]:
current = self.head
while current:
if current.value == value:
return current
current = current.next
return None
def reverse(self) -> None:
prev = None
current = self.head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.head = prev
def has_cycle(self) -> bool:
slow = self.head
fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast:
return True
return False
def cycle_start(self) -> Optional[Node]:
slow = self.head
fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast:
pointer = self.head
while pointer is not slow:
pointer = pointer.next
slow = slow.next
return pointer
return Noneclass Node(object):
def __init__(self, value):
self.value = value
self.next = None
class SinglyLinkedList(object):
def __init__(self):
self.head = None
def prepend(self, value):
node = Node(value)
node.next = self.head
self.head = node
def append(self, value):
node = Node(value)
if not self.head:
self.head = node
return
current = self.head
while current.next:
current = current.next
current.next = node
def delete(self, value):
dummy = Node(0)
dummy.next = self.head
prev = dummy
while prev.next:
if prev.next.value == value:
prev.next = prev.next.next
self.head = dummy.next
return True
prev = prev.next
return False
def reverse(self):
prev = None
current = self.head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.head = prev
def has_cycle(self):
slow = self.head
fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast:
return True
return Falseclass Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
}
prepend(value) {
const node = new Node(value);
node.next = this.head;
this.head = node;
}
append(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
delete(value) {
const dummy = new Node(0);
dummy.next = this.head;
let prev = dummy;
while (prev.next) {
if (prev.next.value === value) {
prev.next = prev.next.next;
this.head = dummy.next;
return true;
}
prev = prev.next;
}
return false;
}
search(value) {
let current = this.head;
while (current) {
if (current.value === value) return current;
current = current.next;
}
return null;
}
reverse() {
let prev = null;
let current = this.head;
while (current) {
const nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
this.head = prev;
}
hasCycle() {
let slow = this.head;
let fast = this.head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
}class ListNode<T> {
value: T;
next: ListNode<T> | null = null;
constructor(value: T) {
this.value = value;
}
}
class SinglyLinkedList<T> {
head: ListNode<T> | null = null;
prepend(value: T): void {
const node = new ListNode(value);
node.next = this.head;
this.head = node;
}
append(value: T): void {
const node = new ListNode(value);
if (!this.head) {
this.head = node;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
delete(value: T): boolean {
const dummy = new ListNode<T>(value);
dummy.next = this.head;
let prev: ListNode<T> = dummy;
while (prev.next) {
if (prev.next.value === value) {
prev.next = prev.next.next;
this.head = dummy.next;
return true;
}
prev = prev.next;
}
return false;
}
reverse(): void {
let prev: ListNode<T> | null = null;
let current = this.head;
while (current) {
const nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
this.head = prev;
}
hasCycle(): boolean {
let slow = this.head;
let fast = this.head;
while (fast && fast.next) {
slow = slow!.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
}public class SinglyLinkedList<T> {
private static class Node<T> {
T value;
Node<T> next;
Node(T value) {
this.value = value;
this.next = null;
}
}
private Node<T> head;
public void prepend(T value) {
Node<T> node = new Node<>(value);
node.next = head;
head = node;
}
public void append(T value) {
Node<T> node = new Node<>(value);
if (head == null) {
head = node;
return;
}
Node<T> current = head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
public boolean delete(T value) {
Node<T> dummy = new Node<>(null);
dummy.next = head;
Node<T> prev = dummy;
while (prev.next != null) {
if (prev.next.value.equals(value)) {
prev.next = prev.next.next;
head = dummy.next;
return true;
}
prev = prev.next;
}
return false;
}
public void reverse() {
Node<T> prev = null;
Node<T> current = head;
while (current != null) {
Node<T> nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
head = prev;
}
public boolean hasCycle() {
Node<T> slow = head;
Node<T> fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
public Node<T> cycleStart() {
Node<T> slow = head;
Node<T> fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
Node<T> pointer = head;
while (pointer != slow) {
pointer = pointer.next;
slow = slow.next;
}
return pointer;
}
}
return null;
}
}#include <optional>
template<typename T>
struct Node {
T value;
Node<T>* next;
explicit Node(T val) : value(val), next(nullptr) {}
};
template<typename T>
class SinglyLinkedList {
public:
Node<T>* head = nullptr;
void prepend(T value) {
Node<T>* node = new Node<T>(value);
node->next = head;
head = node;
}
void append(T value) {
Node<T>* node = new Node<T>(value);
if (!head) { head = node; return; }
Node<T>* current = head;
while (current->next) current = current->next;
current->next = node;
}
bool remove(T value) {
Node<T> dummy(value);
dummy.next = head;
Node<T>* prev = &dummy;
while (prev->next) {
if (prev->next->value == value) {
Node<T>* to_delete = prev->next;
prev->next = prev->next->next;
head = dummy.next;
delete to_delete;
return true;
}
prev = prev->next;
}
return false;
}
void reverse() {
Node<T>* prev = nullptr;
Node<T>* current = head;
while (current) {
Node<T>* next_node = current->next;
current->next = prev;
prev = current;
current = next_node;
}
head = prev;
}
bool hasCycle() {
Node<T>* slow = head;
Node<T>* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
~SinglyLinkedList() {
Node<T>* current = head;
while (current) {
Node<T>* next_node = current->next;
delete current;
current = next_node;
}
}
};#include <stdlib.h>
#include <stdbool.h>
typedef struct Node {
int value;
struct Node* next;
} Node;
Node* create_node(int value) {
Node* node = (Node*)malloc(sizeof(Node));
node->value = value;
node->next = NULL;
return node;
}
void prepend(Node** head, int value) {
Node* node = create_node(value);
node->next = *head;
*head = node;
}
void append(Node** head, int value) {
Node* node = create_node(value);
if (!*head) { *head = node; return; }
Node* current = *head;
while (current->next) current = current->next;
current->next = node;
}
bool delete_node(Node** head, int value) {
Node dummy = {0, *head};
Node* prev = &dummy;
while (prev->next) {
if (prev->next->value == value) {
Node* to_free = prev->next;
prev->next = prev->next->next;
*head = dummy.next;
free(to_free);
return true;
}
prev = prev->next;
}
return false;
}
void reverse(Node** head) {
Node* prev = NULL;
Node* current = *head;
while (current) {
Node* next_node = current->next;
current->next = prev;
prev = current;
current = next_node;
}
*head = prev;
}
bool has_cycle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
void free_list(Node* head) {
while (head) {
Node* next = head->next;
free(head);
head = next;
}
}package linkedlist
type Node struct {
Value int
Next *Node
}
type SinglyLinkedList struct {
Head *Node
}
func (list *SinglyLinkedList) Prepend(value int) {
node := &Node{Value: value, Next: list.Head}
list.Head = node
}
func (list *SinglyLinkedList) Append(value int) {
node := &Node{Value: value}
if list.Head == nil {
list.Head = node
return
}
current := list.Head
for current.Next != nil {
current = current.Next
}
current.Next = node
}
func (list *SinglyLinkedList) Delete(value int) bool {
dummy := &Node{Next: list.Head}
prev := dummy
for prev.Next != nil {
if prev.Next.Value == value {
prev.Next = prev.Next.Next
list.Head = dummy.Next
return true
}
prev = prev.Next
}
return false
}
func (list *SinglyLinkedList) Reverse() {
var prev *Node
current := list.Head
for current != nil {
nextNode := current.Next
current.Next = prev
prev = current
current = nextNode
}
list.Head = prev
}
func (list *SinglyLinkedList) HasCycle() bool {
slow := list.Head
fast := list.Head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
if slow == fast {
return true
}
}
return false
}
func (list *SinglyLinkedList) CycleStart() *Node {
slow := list.Head
fast := list.Head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
if slow == fast {
pointer := list.Head
for pointer != slow {
pointer = pointer.Next
slow = slow.Next
}
return pointer
}
}
return nil
}type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
value: T,
next: Link<T>,
}
pub struct SinglyLinkedList<T> {
head: Link<T>,
}
impl<T: PartialEq> SinglyLinkedList<T> {
pub fn new() -> Self {
SinglyLinkedList { head: None }
}
pub fn prepend(&mut self, value: T) {
let node = Box::new(Node {
value,
next: self.head.take(),
});
self.head = Some(node);
}
pub fn append(&mut self, value: T) {
let node = Box::new(Node { value, next: None });
match self.head {
None => self.head = Some(node),
Some(_) => {
let mut current = self.head.as_mut().unwrap();
while current.next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = Some(node);
}
}
}
pub fn reverse(&mut self) {
let mut prev: Link<T> = None;
let mut current = self.head.take();
while let Some(mut node) = current {
let next = node.next.take();
node.next = prev;
prev = Some(node);
current = next;
}
self.head = prev;
}
pub fn contains(&self, value: &T) -> bool {
let mut current = &self.head;
while let Some(node) = current {
if &node.value == value {
return true;
}
current = &node.next;
}
false
}
}Rust looked at singly linked lists and decided this would be a teaching moment. The ownership model insists that each node have exactly one owner, and a linked list is basically a chain of things that need to own other things that need to own other things. Option<Box<Node<T>>> is the idiomatic solution: Option handles the nullable pointer, Box provides owned heap allocation. The .take() method is key: it moves the value out of the Option and replaces it with None, which is how you transfer ownership without cloning. If you want to understand Rust ownership deeply, there is an entire book called "Learning Rust With Entirely Too Many Linked Lists" that works through six different implementations. It is exactly as painful as it sounds, and it will make you a better Rust programmer.
class Node<T> {
var value: T
var next: Node<T>?
init(_ value: T) {
self.value = value
self.next = nil
}
}
class SinglyLinkedList<T: Equatable> {
var head: Node<T>?
func prepend(_ value: T) {
let node = Node(value)
node.next = head
head = node
}
func append(_ value: T) {
let node = Node(value)
guard let head = head else {
self.head = node
return
}
var current = head
while let next = current.next {
current = next
}
current.next = node
}
@discardableResult
func delete(_ value: T) -> Bool {
guard head != nil else { return false }
let dummy = Node(head!.value)
dummy.next = head
var prev: Node<T> = dummy
while let current = prev.next {
if current.value == value {
prev.next = current.next
self.head = dummy.next
return true
}
prev = current
}
return false
}
func reverse() {
var prev: Node<T>? = nil
var current = head
while let node = current {
let nextNode = node.next
node.next = prev
prev = node
current = nextNode
}
head = prev
}
func hasCycle() -> Bool {
var slow = head
var fast = head
while fast != nil && fast?.next != nil {
slow = slow?.next
fast = fast?.next?.next
if slow === fast { return true }
}
return false
}
}class Node<T>(var value: T, var next: Node<T>? = null)
class SinglyLinkedList<T> {
var head: Node<T>? = null
fun prepend(value: T) {
head = Node(value, head)
}
fun append(value: T) {
val node = Node(value)
if (head == null) { head = node; return }
var current = head!!
while (current.next != null) current = current.next!!
current.next = node
}
fun delete(value: T): Boolean {
if (head == null) return false
val dummy = Node(head!!.value, head)
var prev = dummy
while (prev.next != null) {
if (prev.next!!.value == value) {
prev.next = prev.next!!.next
head = dummy.next
return true
}
prev = prev.next!!
}
return false
}
fun reverse() {
var prev: Node<T>? = null
var current = head
while (current != null) {
val nextNode = current.next
current.next = prev
prev = current
current = nextNode
}
head = prev
}
fun hasCycle(): Boolean {
var slow = head
var fast = head
while (fast != null && fast.next != null) {
slow = slow?.next
fast = fast.next!!.next
if (slow === fast) return true
}
return false
}
}public class Node<T>
{
public T Value;
public Node<T>? Next;
public Node(T value)
{
Value = value;
Next = null;
}
}
public class SinglyLinkedList<T> where T : IEquatable<T>
{
public Node<T>? Head { get; private set; }
public void Prepend(T value)
{
var node = new Node<T>(value) { Next = Head };
Head = node;
}
public void Append(T value)
{
var node = new Node<T>(value);
if (Head == null) { Head = node; return; }
var current = Head;
while (current.Next != null) current = current.Next;
current.Next = node;
}
public bool Delete(T value)
{
var dummy = new Node<T>(default!) { Next = Head };
var prev = dummy;
while (prev.Next != null)
{
if (prev.Next.Value.Equals(value))
{
prev.Next = prev.Next.Next;
Head = dummy.Next;
return true;
}
prev = prev.Next;
}
return false;
}
public void Reverse()
{
Node<T>? prev = null;
var current = Head;
while (current != null)
{
var nextNode = current.Next;
current.Next = prev;
prev = current;
current = nextNode;
}
Head = prev;
}
public bool HasCycle()
{
var slow = Head;
var fast = Head;
while (fast != null && fast.Next != null)
{
slow = slow!.Next;
fast = fast.Next.Next;
if (ReferenceEquals(slow, fast)) return true;
}
return false;
}
}class Node
attr_accessor :value, :next_node
def initialize(value)
@value = value
@next_node = nil
end
end
class SinglyLinkedList
attr_accessor :head
def initialize
@head = nil
end
def prepend(value)
node = Node.new(value)
node.next_node = @head
@head = node
end
def append(value)
node = Node.new(value)
if @head.nil?
@head = node
return
end
current = @head
current = current.next_node while current.next_node
current.next_node = node
end
def delete(value)
dummy = Node.new(nil)
dummy.next_node = @head
prev = dummy
while prev.next_node
if prev.next_node.value == value
prev.next_node = prev.next_node.next_node
@head = dummy.next_node
return true
end
prev = prev.next_node
end
false
end
def reverse
prev = nil
current = @head
while current
next_node = current.next_node
current.next_node = prev
prev = current
current = next_node
end
@head = prev
end
def has_cycle?
slow = @head
fast = @head
while fast && fast.next_node
slow = slow.next_node
fast = fast.next_node.next_node
return true if slow.equal?(fast)
end
false
end
end<?php
class Node {
public mixed $value;
public ?Node $next;
public function __construct(mixed $value) {
$this->value = $value;
$this->next = null;
}
}
class SinglyLinkedList {
public ?Node $head = null;
public function prepend(mixed $value): void {
$node = new Node($value);
$node->next = $this->head;
$this->head = $node;
}
public function append(mixed $value): void {
$node = new Node($value);
if ($this->head === null) {
$this->head = $node;
return;
}
$current = $this->head;
while ($current->next !== null) {
$current = $current->next;
}
$current->next = $node;
}
public function delete(mixed $value): bool {
$dummy = new Node(null);
$dummy->next = $this->head;
$prev = $dummy;
while ($prev->next !== null) {
if ($prev->next->value === $value) {
$prev->next = $prev->next->next;
$this->head = $dummy->next;
return true;
}
$prev = $prev->next;
}
return false;
}
public function reverse(): void {
$prev = null;
$current = $this->head;
while ($current !== null) {
$nextNode = $current->next;
$current->next = $prev;
$prev = $current;
$current = $nextNode;
}
$this->head = $prev;
}
public function hasCycle(): bool {
$slow = $this->head;
$fast = $this->head;
while ($fast !== null && $fast->next !== null) {
$slow = $slow->next;
$fast = $fast->next->next;
if ($slow === $fast) return true;
}
return false;
}
}What Problems It Solves
The singly linked list shows up when you need:
Efficient front insertion and deletion. If your workload is predominantly adding and removing from the front of a collection, a linked list gives you O(1) for both. Arrays pay O(n) to shift elements on every front insertion.
Dynamic-size collections with unpredictable growth. Arrays require contiguous memory. A linked list node can live anywhere on the heap, so you never need to reallocate-and-copy as the collection grows.
Implementing other data structures. Stacks, queues, and adjacency lists in graphs are all naturally expressed as linked lists. Hash table chaining (each bucket is a linked list of colliding entries) is another classic application.
LRU cache backing. The most common LRU cache implementation pairs a hash map with a doubly linked list. The linked list orders nodes by recency; the map provides O(1) lookup. Eviction is O(1): just remove the tail. Promotion on cache hit is O(1): remove the node from its position and reattach it at the head.
OS and runtime internals. The Linux kernel uses linked lists pervasively for process lists, memory descriptors, and file system structures. The kernel's implementation is notable: instead of embedding data in the node, it embeds the node (list_head) in the data struct, then uses container_of to recover the outer struct from the node pointer.
Recognizing Linked List Interview Patterns
Half the battle in an interview is recognizing that the problem in front of you wants a linked list technique at all. The prompt often won't say "linked list." It'll just describe a scenario that screams it.
Signals that a problem wants linked list technique, even when the prompt never says "linked list":
- You are given a head node and asked to modify the structure without extra data structures
- The problem says "in-place" and "O(1) space" in the same breath
- You need to find the middle, detect a cycle, or find the kth element from the end
- You need to merge or partition based on a condition
- The problem involves a "runner" technique where two pointers advance at different rates
Worked Example 1: Palindrome Check
Problem: Given a singly linked list, determine whether it is a palindrome. O(n) time, O(1) extra space.
Why a linked list technique fits: You cannot index from both ends like you can with an array. The O(1) space constraint rules out copying values into an array or stack. You have to work with the structure itself, which means reaching for patterns you just learned.
Approach:
- Use fast/slow pointers to find the middle.
- Reverse the second half in-place (the in-place reversal pattern).
- Compare the first half and the reversed second half node by node.
- Optionally restore the list by reversing the second half again.
def is_palindrome(head):
if not head or not head.next:
return True
# find middle
slow = head
fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
# reverse second half
second_half = slow.next
prev = None
current = second_half
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
reversed_second = prev
# compare
pointer1 = head
pointer2 = reversed_second
result = True
while pointer2:
if pointer1.value != pointer2.value:
result = False
break
pointer1 = pointer1.next
pointer2 = pointer2.next
return resultTwo patterns in one problem: fast/slow for the middle, in-place reversal for the comparison. This kind of pattern composition is exactly what hard interviews are testing. If you can explain why each piece is there, you're not just solving it, you're narrating it, which is what separates a pass from a hire.
Worked Example 2: Remove Nth Node From End
Problem: Remove the nth node from the end of the list in one pass. O(n) time, O(1) space.
Why a linked list technique fits: "From the end" sounds like you need to know the length first, which costs a second pass. The "one pass" constraint is the hint. One pass with what? Two pointers.
Approach: Use two pointers with a gap of n between them. When the leading pointer reaches the end, the trailing pointer is at the node just before the one to delete.
def remove_nth_from_end(head, n):
dummy = ListNode(0)
dummy.next = head
fast = dummy
slow = dummy
# advance fast n+1 steps to create a gap of n
for _ in range(n + 1):
fast = fast.next
# move both until fast reaches the end
while fast:
slow = slow.next
fast = fast.next
# slow is now the predecessor of the node to remove
slow.next = slow.next.next
return dummy.nextThe dummy head is pulling its weight here. When n equals the list length, slow lands on the dummy, which makes removing the real head just dummy.next = dummy.next.next. Without the dummy you need a special case. You always need a special case. That's why you add the dummy.
Recap
- A node is value plus a
nextpointer. The list is just the head pointer. Lose the head pointer, lose everything. - Nodes are heap-allocated and scattered in memory. Cache misses are the price you pay for structural flexibility. The CPU is not impressed by your O(1) inserts.
- Prepend and delete-at-head are O(1). Everything else requires traversal: O(n).
- The dummy head node eliminates boundary special cases. Use it habitually, not just when you remember it.
- In-place reversal needs exactly three pointers:
prev,curr,next. Savenextbefore overwritingcurr.next. That is the only line that can end your list. - The iterative reversal is O(1) space. The recursive version is O(n) call stack. Know both; use the iterative in interviews.
- Fast/slow pointers detect cycles in O(n) time and O(1) space.
- When fast and slow meet inside a cycle, reset one pointer to head and walk both one step at a time. They converge at the cycle's entry. The proof is
F = kC - a. - Recognize the signals: "in-place", "O(1) space", "nth from end", "middle", "cycle" all point at linked list pointer techniques.
Reading about linked lists and surviving a linked list question in a live interview are two very different experiences. If you want to practice explaining these techniques out loud, with a clock running and someone asking follow-ups, SpaceComplexity runs voice-based mock interviews that cover exactly this: pointer problems, in-place transformations, and the follow-up questions interviewers ask after you write the code. Try a session before your next loop.
For more on interview communication patterns that go alongside your technical skills, see You Solved the Problem. So Why No Offer? and Don't Go Silent.
Further Reading
- Linked list (Wikipedia), canonical reference covering variants, history, and implementation trade-offs
- Floyd's Cycle-Finding Algorithm (cp-algorithms.com), clean mathematical treatment of tortoise-and-hare with full proof
- std::forward_list (cppreference.com), C++ singly linked list, with complexity guarantees for every operation
- Learning Rust With Entirely Too Many Linked Lists, ownership semantics through the lens of six different linked list implementations; essential Rust reading
- Linked List Data Structure (GeeksforGeeks), broad reference with diagrams and language examples
