Wiki
Core13 min read

Linked lists

Nodes joined by next pointers trade cheap indexing for cheap insertion, at the cost of chasing memory.

Inserting in the middle of an array is expensive because everything after the insertion point must move. A linked list removes that cost by refusing to keep its elements together. Each element lives in its own node with a pointer to the next, so changing the chain means rewriting a pointer or two — at the price of losing random access entirely.

A treasure hunt, not a shelf

An array is a shelf: you can reach the seventh slot directly. A linked list is a treasure hunt: each clue tells you where the next one is. You can insert a clue by editing the previous one, but you cannot jump to the seventh clue without following six pointers.

Use the panel to prepend, append, remove the head and walk the chain. The cursor shows how traversal actually moves.

Insert, remove and walk the chain of next pointers

head→

7

next 4

→

4

next None

→
None

head -> 7 -> 4 -> None

Press walk to place the cursor on the head.

Nothing is indexed: to find the third element you follow three pointers. Inserting at the head rewrites one pointer no matter how long the list is, but reaching the tail costs a full walk — the exact trade-off arrays make in reverse.

The node

A node is a tiny record: some data and a reference to the next node (or None at the end). The list itself is just a reference to the head.

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next
 
class LinkedList:
    def __init__(self):
        self.head = None
 
    def prepend(self, value):
        self.head = Node(value, self.head)   # O(1)

prepend rewrites one pointer regardless of the list length, which is exactly what an array cannot do.

The costs

  • Prepend at the head: O(1)O(1).
  • Insert or delete after a known node: O(1)O(1).
  • Search / access index k: O(n)O(n) — you must walk.
  • Append without a tail pointer: O(n)O(n); keep a tail reference to make it O(1)O(1).

Notice the trade is the mirror image of the array: cheap anywhere insertion, expensive indexing. Neither is better; they are good at opposite things.

Variants

  • A doubly linked list adds a prev pointer, so you can delete a node given only the node itself, and traverse backward. Each node pays one extra pointer.
  • A circular list points the tail back at the head, useful for round-robin scheduling.
  • A sentinel node at the front removes the special case of inserting into an empty list, simplifying the code.

Losing the head loses the list

If you reassign self.head without saving the old value, the rest of the nodes become unreachable and are garbage-collected. In a doubly linked list, forgetting to update both prev and next during a splice is the classic source of subtle bugs — draw the pointers before you write the update.

Linked lists matter less in Python than in C because a Python list is already a fast dynamic array, but the pointer-chasing pattern behind them reappears in every tree and graph you will meet later.

Illustrative vs real

The walker shows a short chain of integers so the pointers fit on screen and are visible as arrows. Real nodes hold arbitrary objects and may be scattered across memory; that scattering is precisely why they are cache-unfriendly compared with a contiguous array. The asymptotic costs are unaffected.

Check yourself

Eduspheria wiki · Programming & Data Structures, Data structures

0 / 5 answered

  1. 1What is the cost of inserting a new node at the head of a singly linked list?
    Multiple choice
  2. 2Accessing the k-th element of a linked list is O(1).
    True / false
  3. 3What is the pointer at the end of a singly linked list set to?
    Short answer
  4. 4Which additional pointer does a doubly linked list node carry?
    Multiple choice
  5. 5How many pointer fields change when prepending to a non-empty singly linked list?
    fields
    Numeric answer

From the assignment paper

Modeled on NITJ AI-507, Assignment/Quiz

0 / 5 answered

  1. 1What is the main advantage of a doubly linked list over a singly linked list?
    Multiple choice
  2. 2After locating the node holding a given key in a singly linked list, what must a deletion change?
    Multiple choice
  3. 3Which operation is not part of the stack abstract data type?
    Multiple choice
  4. 4What is the postfix form of the infix expression (A + B) * (C - D)?
    Multiple choice
  5. 5Which structure naturally models the undo history of a text editor?
    Multiple choice

Where next: trees — imposing an order on linked nodes so search becomes logarithmic instead of linear.