Jaconir

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.

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
You are given the heads of two sorted linked lists, `list1` and `list2`. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Go Deeper with "Cracking the Coding Interview"

For a comprehensive guide to data structures like linked lists and the interview problems associated with them, "Cracking the Coding Interview" by Gayle Laakmann McDowell is an essential resource.