How Linked Lists Work — API Cheat Sheet
No exercise here — what a linked list actually is, which languages have a built-in one, and the handful of node operations interview problems reuse over and over.
This is a reference page, not a graded exercise.
A linked list is a chain of nodes, where each node holds a value and a pointer to the next node (and, for a doubly-linked list, a pointer to the previous one too). Unlike an array, there's no contiguous block of memory and no O(1) random access by index — you can only get to node i by walking there from the head, one .next at a time.
Important for interviews: almost every linked-list interview problem (reverse a list, detect a cycle, merge two lists) expects you to write your own tiny node class and manipulate raw pointers by hand — that's the whole point of the exercise. The built-in linked-list classes below are useful to know exist, but they're rarely what you use to solve a "reverse this list" style question.
The node class you'll write yourself (all languages)
// Java
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
// C#
public class ListNode {
public int val;
public ListNode next;
public ListNode(int val) { this.val = val; }
}
# Python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
// JavaScript / TypeScript
class ListNode {
val: number;
next: ListNode | null;
constructor(val: number, next: ListNode | null = null) {
this.val = val;
this.next = next;
}
}
Core operations you'll reuse constantly
| Want to... | Pattern |
|---|---|
| Traverse | while (node != null) { ...; node = node.next; } |
| Insert after a node | newNode.next = node.next; node.next = newNode; |
Delete the node after node | node.next = node.next.next; |
| Reverse the whole list | walk with prev = null, curr = head; each step: next = curr.next; curr.next = prev; prev = curr; curr = next; |
| Detect a cycle | two pointers, slow (1 step) and fast (2 steps) — if they meet, there's a cycle (Floyd's algorithm) |
If your language happens to have a built-in one
| Language | Built-in class | Notes |
|---|---|---|
| Java | java.util.LinkedList<T> | implements both List and Deque — addFirst/addLast/removeFirst/removeLast/peekFirst/peekLast |
| C# | System.Collections.Generic.LinkedList<T> | nodes are LinkedListNode<T> with .Value, .Next, .Previous; list has .AddFirst/.AddLast/.RemoveFirst/.RemoveLast |
| Python | none built-in | collections.deque behaves like a doubly-linked list for O(1) appendleft/append/popleft/pop, but for interview-style singly-linked-list problems you define your own ListNode |
| JavaScript / TypeScript | none built-in | always define your own ListNode class for interview problems |
The gotcha that costs the most time: forgetting to null-check before dereferencing .next (node.next.next when node.next might be null), and losing the reference to a node you still need before you overwrite its .next pointer. When reversing a list, save curr.next into a temp variable before you reassign curr.next = prev.
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
This question doesn't have a code exercise.