Skip to main content
Quick Reference

Linked List Cheatsheet

A linked list on one page — nodes, splices, and the traps around the head, lost tails, and cycles.

10 sections 10 tables 14 code snippets

A node and a head

One value, one link

class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

Walk until the end

while node:
    node = node.next

The parts

PartMeaning
NodeA value plus a next link
HeadThe first node. An empty list has head None
NextThe following node, or None on the last node
Index ii hops from the head. An array slot is O(1)
LastThe node whose next is None
MemoryEach node is its own object, so a scan is not one contiguous block

This page is a linked list. A tree is left out.

What each operation costs

New front in two writes

node.next = head
head = node

Cost from the head

OpCost
Insert at headO(1). Point the new node at the old head, then move head
Delete headO(1). head = head.next
Append, no tailO(n). Walk to the last node
Append, with tailO(1), and the first node must set head and tail
SearchO(n). Values are not indexed
SpaceO(n) nodes, plus one link on each node

Head and tail

The empty list is special

# the first node sets both head and tail

Keep the two ends honest

CaseWhat to update
Empty inserthead and tail both become the new node
Appendtail.next = node, then tail = node
Delete the only nodehead and tail both become None
Delete head, more remainhead = head.next. tail stays
Insert at head, list already has nodeshead moves. tail stays
Queue shapeDequeue is delete-head. Enqueue is append. Both are O(1) with a tail

Insert after a node

Save the rest, then link

fresh.next = node.next
node.next = fresh

Order of the two writes

StepWhy
Save the old nextfresh.next = node.next keeps every later node
Then link forwardnode.next = fresh
Opposite orderThe nodes after node become unreachable
Before a nodeA singly list needs the previous node, so this is a walk
After a node you holdO(1). You already have the predecessor
Insert at index iWalk i steps, then splice. The walk is O(n)

Unlink a node

Skip the current node

# prev.next = curr.next

Who must change

CaseMove
Middleprev.next = curr.next
Headhead = head.next. There is no prev
Last nodeThe new last next is None, and a stored tail must move too
By valueWalk until curr.val matches, then unlink. O(n)
Copy trickOn a non-tail node you hold, copy the next value here and skip that next node
Tail you holdThere is no next value to copy, so the trick fails

Reverse the links

Save next, then turn the link

nxt = curr.next
curr.next = prev
prev = curr
curr = nxt

One pass, constant extra space

FactMeaning
Save next firstcurr.next = prev overwrites the only forward link
Then slideprev = curr and curr = nxt
New headprev, once curr becomes None
Empty or one nodeThe loop leaves that same head
TimeO(n)
Extra spaceO(1) iterative. A recursive reverse uses O(n) stack frames

Slow and fast

Second middle on an even count

while fast and fast.next:
    slow = slow.next
    fast = fast.next.next

Nth from the end

# gap of n, then step until fast is None

Two speeds

GoalMove
Middle, odd countslow ends on the center
Middle, even countThis loop returns the second middle
FastTwo steps. Stop when fast or fast.next is None
Nth from the endMove fast n steps from the head, then move both until fast is None. slow is on that node
Step off Nonen is longer than the list
DummyA fake node before head lets one unlink path delete the head too. Return dummy.next

A cycle

Meet, then find the entrance

# after they meet, put one pointer back on the head

Floyd uses constant extra memory

CheckMeaning
No cyclefast or fast.next becomes None
Cycleslow and fast land on the same node
Why they meetInside the cycle, fast gains one node on slow each turn
EntranceAfter the meet, one pointer returns to the head. Both then step once per turn
Where they meet nextThe first node of the cycle
Visit setAlso detects a cycle, and uses O(n) extra memory. Floyd uses O(1)

Doubly and circular

Middle node, both neighbors

# node.prev.next = node.next
# then node.next.prev = node.prev

A second link changes the cost

KindDifference
SinglyOne next link. Unlinking a middle node means changing the previous next
Doublyprev and next. A known node unlinks in O(1)
Insert betweenFour writes: the new node and both neighbors
Delete headhead = node.next, and the new head prev becomes None
CircularThe last next points at the head. A walk cannot stop on None
One-node circlenode.next is node. Stop when the walk returns to the start

Interview traps

The rest of the list drops

# node.next = fresh first loses every later node

An array shift is not a splice

# list.insert(0, x) shifts an array

A circle has no None end

# a circular tail points at the head, so next is never None

Traps worth memorising

TrapWhat happensPrefer
Link forward firstThe rest of the list dropsSet fresh.next first
Forget head = nodeThe new front node is unreachableMove head after the link
None.nextAttributeErrorTest the node before the read
Reverse without saving nextThe tail of the list is gonenxt = curr.next before the turn
list.insert(0, x)An array shifts. It is not a pointer rewireA Node when you need links
Walk until None on a circleThe loop never endsStop when you return to the start

How to use this page: insert at the head in O(1), set fresh.next before node.next, and save next before you reverse a link.