Jaconir
Skip to lesson
easy
25 minMostly intern / new-grad

When to use. O(1) insert/delete once you hold the node; streaming; when arrays would shift too much.

Complexity. Time Access O(n); insert/delete at a known node O(1). Space O(1) extra for most rewires; O(n) if you copy.

Who gets asked. Still a staple for intern and new-grad screens. Senior rounds rarely start here unless the role is systems-heavy.

Prereq. Arrays — know why O(1) index access is special.

Linked Lists: Chains of Data

Dive into linked lists, the dynamic alternative to arrays that excels at insertions and deletions.

What Is a Linked List?

Imagine a scavenger hunt. You get a clue that tells you where the next clue is hidden. You can't just jump to the final prize; you have to follow the chain of clues one by one. A linked list works in a very similar way. It's a sequence of "nodes," where each node contains two pieces of information:

  • Data: The actual value being stored (like a number, a string, or an object).
  • A Pointer: A reference (or "link") to the very next node in the sequence.

The first node is called the Head, and the last node's pointer is typically `null`, signaling the end of the list. Unlike arrays, nodes in a linked list are not stored in contiguous memory. They can be scattered all over, connected only by the pointers. This structure gives them unique performance characteristics. Access is O(n); insert/delete at a known node is O(1). Reverse, merge two sorted lists, and fast/slow cycle detection are intern-loop staples. Senior rounds rarely start here unless the role is systems-heavy. Fast/slow is the same idea as two pointers.

Interactive Linked List
Add and remove nodes to see how a linked list works.
A
HEAD
B
C
Array vs. Linked List: Visual Time Complexity
See why some operations are faster on different data structures.

Arrays offer instant O(1) access. Linked lists must be traversed from the head, making access an O(n) operation.

Array:
O(1)

10
20
30
40

Linked List:
O(n)

10
20
30
40
Problem: Reverse a Linked List
A classic interview question. The goal is to reverse the list in-place by manipulating pointers, not by creating a new list.
A
B
C
Prev: nullCurrent: ANext: B

Start. `prev` is null, `current` is at Head (A).

Quick Quiz: Test Your Knowledge

What is the time complexity to access the 5th element in a singly linked list?

What is the main advantage of a linked list over an array?

Each node in a singly linked list contains data and a pointer to what?

Coding Challenge
Two sorted singly linked lists. Merge into one sorted list by rewiring nodes (dummy head). O(n+m) time, O(1) extra.

Saved in this browser. No account.