Linked List Ready
length 4 head → 10
  • Current
  • Comparing
  • New / found
  • Removing
  • Changed link
  • Removed link

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

Application · linked lists in action

Polynomials with linked lists

A polynomial such as 3x³ + 2x + 5 can have gaps (no x² term) and any length, so an array wastes space or needs a big size. A linked list stores one node per term — coef | exp | next — sorted from the highest exponent down. Adding two polynomials becomes a merge of two sorted lists. Type your own polynomials and try each operation.

Polynomial Ready
one node = one term · highest exponent first
  • Current
  • Comparing
  • New / updated
  • Removing
  • Changed link

Ready

Comparisons 0 Shifts 0
Ready

Write terms like 3x^3 -x 7 · up to 4 terms, exponents 0–5 · keyboard shortcuts work on the visualizer you used last.

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

What is a node?

Each node stores a value (data) and a pointer (next) to the following node. The list only remembers where the first node is — the head. In a singly list the last node points to NULL. A doubly node also has a prev pointer, and in a circular list the last node points back to head. A doubly circular list does both: head.prev is the last node and last.next is head. For polynomials the node holds a whole term: a coefficient and an exponent.

// Singly and circular lists
struct Node {
    int data;            // the value
    struct Node *next;   // address of the next node
};

// Doubly and doubly circular lists
struct DNode {
    struct DNode *prev;  // address of the previous node
    int data;
    struct DNode *next;
};

// Polynomials: one node = one term, e.g. 3x^2
struct Term {
    int coef;            // 3
    int exp;             // 2
    struct Term *next;   // the term with the next lower exponent
};

struct Node *head = NULL;   // empty list

Insert at beginning

  1. Create a new node with the value.
  2. Point the new node to the current head.
  3. Move head to the new node.
  4. Only 2 pointer changes — O(1).

Insert at end / position

  1. Create a new node.
  2. Walk with temp to the node before the spot.
  3. newNode.next = temp.next (do this first!).
  4. temp.next = newNode.

Delete a node

  1. Walk with temp to the node before the target.
  2. Point temp.next past the target.
  3. Free the target node's memory.
  4. Deleting the head is O(1) — just move head.

Reverse

  1. Keep three pointers: prev, curr, next.
  2. Save next = curr.next.
  3. Flip the link: curr.next = prev.
  4. Move prev and curr one step; finally head = prev.

Doubly: two directions

  1. Every node also has a prev pointer.
  2. Every insert or delete fixes BOTH next and prev.
  3. You can walk backward from the last node.
  4. Delete needs no "node before" walk — use prev.

Circular: no end

  1. The last node points back to head, not NULL.
  2. Loops stop when curr comes back to head.
  3. Changing head means updating the last node too.
  4. A single node points to itself.

Doubly circular: no walking

  1. head.prev is the last node, and last.next is head.
  2. So the last node is found in O(1) — no loop.
  3. Insert or delete at either end changes 4 pointers.
  4. A single node points to itself in both directions.

Polynomial: one node per term

  1. Each node is [coef | exp | next], e.g. 3x² = [3 | 2].
  2. Keep the list sorted: highest exponent first.
  3. One node per exponent — like terms are combined.
  4. A term with coefficient 0 is never stored.

Polynomial: add or subtract

  1. Put p on P and q on Q, like merging two sorted lists.
  2. Higher exponent: copy that term, move that pointer.
  3. Equal exponents: add (or subtract) the coefficients; skip the term if the result is 0.
  4. When one list ends, copy the rest of the other.

Polynomial: multiply

  1. Take every term of P and multiply it by every term of Q.
  2. Multiply the coefficients, ADD the exponents.
  3. Insert each product into the sorted result.
  4. If its exponent is already there, add the coefficients instead.

Polynomial: evaluate P(x)

  1. Start with sum = 0 and curr = head.
  2. For each node compute coef × x^exp.
  3. Add it to sum and move to the next node.
  4. When curr is NULL, sum is P(x).
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 INSERT-FIRST(head, value)
2 newNode ← new Node(value)
3 newNode.next ← head
4 head ← newNode
Complexity

How fast is each operation?

n = number of nodes. Every list operation uses O(1) extra space. For polynomials, m and n are the numbers of terms in P and Q.

Operation Best Average Worst Speed
Insert at beginning O(1) O(1) O(1) Instant
Insert at end O(n) O(n) O(n) Grows with n
Insert at position O(1) O(n) O(n) Grows with n
Delete at beginning O(1) O(1) O(1) Instant
Delete at end O(n) O(n) O(n) Grows with n
Search O(1) O(n) O(n) Grows with n
Access i-th node O(1) O(n) O(n) Grows with n
Reverse O(n) O(n) O(n) Grows with n
Doubly circular: insert / delete at either end O(1) O(1) O(1) Instant
Polynomial: add or subtract (m and n terms) O(m + n) O(m + n) O(m + n) Grows with n
Polynomial: multiply (m and n terms) O(m · n) O(m · n · (m + n)) O(m · n · (m + n)) Grows with n
Polynomial: evaluate P(x) O(n) O(n) O(n) Grows with n

Singly vs Doubly vs Circular vs Doubly circular

Singly Doubly Circular Doubly circular
Pointers per node next prev + next next prev + next
Last node points to NULL NULL head head (and head.prev = last)
Walk backward? No Yes No Yes — and it wraps around
Insert at beginning O(1) O(1) O(n) — must update the last node* O(1) — head.prev is the last node
Insert / delete at the end O(n) O(n) O(n) O(1) — head.prev is the last node
Delete a node you already have O(n) — need the node before it O(1) — use prev O(n) — need the node before it O(1) — use prev
Extra memory 1 pointer per node 2 pointers per node 1 pointer per node 2 pointers per node
Typical use Stacks, simple lists Browser back / forward, undo & redo Round-robin turns, playlists on repeat Music player (next, previous, repeat), rotating schedules

* With an extra tail pointer, a circular list can insert at the beginning in O(1). A doubly circular list needs no tail pointer: head.prev already is the tail.

Polynomials: array vs linked list

Array (one slot per power) Linked list (one node per term)
Stores 5x¹⁰⁰ + 3 101 slots, almost all zero 2 nodes
Size needed Highest exponent + 1 Number of terms
Add two polynomials O(highest exponent) O(m + n) — only the real terms
Coefficient of xᵏ O(1) — poly[k] O(n) — walk the list
Best for Dense polynomials (most powers present) Sparse polynomials (few terms, big gaps)

Neither is always better: a list wins when most powers are missing, an array wins when most are present.

Array vs Linked List

Array Linked List
Memory One block, side by side Nodes anywhere, joined by pointers
Size Fixed capacity Grows and shrinks freely
Access the i-th element O(1) — calculate the address O(n) — follow pointers from head
Insert at the front O(n) — shift everything O(1) — change 2 pointers
Extra memory None One pointer per node
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 12 correct
Question 1

What does the next pointer of the LAST node hold?

Question 2

What is the time complexity of inserting at the beginning of a linked list?

Question 3

Inserting a new node after temp: which step must happen FIRST?

Question 4

Why can't a linked list jump straight to the 5th node like an array can?

Question 5

In a doubly linked list, what does head.prev hold?

Question 6

How does a loop over a circular linked list know when to stop?

Question 7

In a doubly circular linked list with 4 nodes, what does head.prev hold?

Question 8

Which operation is O(1) in a doubly circular list, but O(n) in a singly circular list that has no tail pointer?

Question 9

Which fields does one node of a polynomial linked list hold?

Question 10

Adding 3x² + 2x and 5x + 1: both pointers reach a term with exponent 1. What happens?

Question 11

Why is a polynomial list kept sorted by exponent?

Question 12

Multiplying the terms 3x² and 4x³ gives which term?

Assessment

Linked Lists quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.