See Algorithms
SEE ALGORITHMS

Linked List Visualization

A Linked List is a linear data structure where elements are stored in standalone nodes, each holding a data value and a pointer to the next node in sequence. Unlike arrays, linked lists do not require contiguous memory allocation and can dynamically expand and contract without expensive resizing or reallocation. This makes insertions and deletions at known positions strictly constant time O(1), though random access requires a linear traversal.

Visualizer



Pseudocode

function insertAtHead(value):
    node = new Node(value)
    node.next = head.next
    head.next = node
function insertAtTail(value):
    node = new Node(value)
    cur = head
    while cur.next is not null:
        cur = cur.next
    cur.next = node
function insertAt(index, value):
    if index == 0:
        insertAtHead(value)
        return
    cur = head
    for i = 1 to index:
        if cur.next is null: break
        cur = cur.next
    node = new Node(value)
    node.next = cur.next
    cur.next = node
function deleteAt(index):
    cur = head
    prev = null
    for i = 0 to index:
        prev = cur
        cur = cur.next
    prev.next = cur.next

Key Properties

  • Dynamic Structure: Allocates memory dynamically for individual nodes at runtime, expanding and contracting without predefined capacity or costly array reallocations.
  • Non-Contiguous Storage: Nodes are stored independently across heap memory and connected via pointers, eliminating the need for large contiguous memory blocks.
  • Sequential Access: Direct random access by index is not supported; locating an element requires traversing from the head node in O(n) time.
  • Efficient Insertion & Deletion: Inserting or removing elements at known positions requires only pointer updates in constant O(1) time without shifting elements.
  • Pointer Overhead: Every node incurs additional memory overhead to store reference pointers alongside its data payload.

When to Use

Linked lists are ideal when the total number of items is unknown in advance, frequent insertions or deletions occur at the beginning of the collection (such as implementing Stacks or Queues), or memory fragmentation makes allocating a large contiguous array impossible. They also form the standard structure for graph adjacency lists and hash table collision chaining. When constant-time random access (arr[i]) or cache-friendly sequential iteration is critical, contiguous arrays should be preferred.

Time & Space Complexity

Metric / Operation
Complexity
Description
Access by IndexO(n)Must traverse sequentially from the head node to the target index.
Search by ValueO(n)Requires a linear scan through the chain comparing values.
Insert / Delete at HeadO(1)Updates the head pointer and next reference in constant time without element shifting.
Insert / Delete at TailO(n)Traverses to the end unless a direct tail pointer is maintained.
Insert / Delete at IndexO(n)Takes O(n) to traverse to the position, followed by an O(1) pointer assignment.
Space ComplexityO(n)Stores n data items with one pointer reference per node overhead.

Common Interview Questions

Compare array access vs Linked List access in terms of memory cache performance.

Arrays store elements in contiguous memory blocks, enabling O(1) index access and excellent CPU spatial cache prefetching. Linked list nodes are allocated dynamically across heap memory, leading to memory fragmentation and frequent CPU cache misses.

How do you detect a cycle in a Linked List using Floyd's Cycle-Finding algorithm (Tortoise and Hare)?

Use two pointers: slow moving 1 step at a time, and fast moving 2 steps at a time. If the list has a cycle, fast will eventually catch up and meet slow inside the loop.

Code Snippet
let slow = head, fast = head;
while (fast && fast.next) {
  slow = slow.next;
  fast = fast.next.next;
  if (slow == fast) return true; // Cycle detected
}
return false;
How do you reverse a Linked List in-place in O(n) time and O(1) space?

Maintain three pointers: prev (initialized to null), curr (head), and next. Iterate through the list:

Code Snippet
let prev = null, cur = head;
while (cur) {
  let next = cur.next;
  cur.next = prev;
  prev = cur, cur = next;
}
head = prev;

💬  Discussion

to join the discussion


Curious to Learn More?

Hand-picked resources to deepen your understanding


Learn DSA on Udemy
Learn DSA on Udemy
As an Udemy Associate, I earn from qualifying purchases.

© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0