Skip to content

Singly Linked List

Medium ~35 min

What you'll build

A LinkedList class whose nodes hold a value and a next pointer, supporting append, prepend, delete, and toArray operations.

Goals

  • Model a node as an object with value and next references
  • Traverse a linked list by following next pointers
  • Insert and remove nodes while keeping the chain intact
  • Compare linked list trade-offs against arrays

Prerequisites

  • Objects and property access
  • Functions and return values
  • while loops

Steps

  1. 1

    Define the Node and List

    A linked list is a chain of nodes. Each node stores a value and a reference to the next node. Create a Node helper and a LinkedList class with a head pointer that starts null.

    Code
    class Node {
      constructor(value) {
        this.value = value;
        this.next = null;
      }
    }
    
    class LinkedList {
      constructor() {
        this.head = null;
        this.size = 0;
      }
    }

    Expected result

    An empty LinkedList with head === null and size === 0.

  2. 2

    Implement append

    To append at the tail, walk from head to the last node (where next === null), then attach the new node. If the list is empty, the new node becomes the head.

    Code
    append(value) {
      const node = new Node(value);
      if (!this.head) {
        this.head = node;
      } else {
        let current = this.head;
        while (current.next) current = current.next;
        current.next = node;
      }
      this.size++;
      return this;
    }

    Expected result

    list.append(1).append(2).append(3) builds a chain 1 → 2 → 3.

  3. 3

    Implement prepend

    To prepend at the head, point the new node's next at the current head, then make the new node the head. This is O(1) — faster than append.

    Code
    prepend(value) {
      const node = new Node(value);
      node.next = this.head;
      this.head = node;
      this.size++;
      return this;
    }

    Expected result

    list.prepend(0) makes 0 the new head, shifting the rest right.

  4. 4

    Implement delete

    To delete a value, walk the list keeping a reference to the previous node. When the current node matches, unlink it by pointing prev.next at current.next. Handle the head case separately.

    Code
    delete(value) {
      if (!this.head) return false;
      if (this.head.value === value) {
        this.head = this.head.next;
        this.size--;
        return true;
      }
      let prev = this.head;
      let current = this.head.next;
      while (current) {
        if (current.value === value) {
          prev.next = current.next;
          this.size--;
          return true;
        }
        prev = current;
        current = current.next;
      }
      return false;
    }

    Expected result

    After delete(2), the chain skips the node whose value was 2.

  5. 5

    Implement toArray and test

    Add a toArray method that walks the list and collects values into an array, then run a full end-to-end test.

    Code
    toArray() {
      const result = [];
      let current = this.head;
      while (current) {
        result.push(current.value);
        current = current.next;
      }
      return result;
    }
    
    // Test
    const list = new LinkedList();
    list.append(1).append(2).append(3);
    list.prepend(0);
    console.log(list.toArray()); // [0, 1, 2, 3]
    list.delete(2);
    console.log(list.toArray()); // [0, 1, 3]
    console.log('Size:', list.size); // 3

    Expected result

    Output shows [0,1,2,3], then [0,1,3], then Size: 3.

Try it yourself

What you learned

You built a node-based data structure and practiced pointer manipulation — the foundation for trees, graphs, and other linked structures that arrays cannot easily represent.

Related Resources

Related Lessons

  • Objects

    Each node is an object with value and next references.

  • Loops

    Traversal uses a while loop following next pointers.

Related Glossary

  • Linked List

    A chain of nodes connected by references.

  • Pointer

    A reference to another node or object.

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

  • Linked List

    A chain of nodes connected by references.

  • Pointer

    A reference to another node or object.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic