Linked list in JavaScript & JS: ListNode, build, traverse (linked list javascript)

linked list javascript, linked list js, javascript linked list, linked list in javascript, js linked list, linkedlist javascript, javascript linked lists, listnode js, javascript listnode, how to create a linked list in javascript: singly linked ListNode, prepend, forward build, traverse, class remove.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Linked list in JavaScript & JS: ListNode, build, traverse (linked list javascript)

A singly linked list is a chain of nodes where each node holds a value (often val or data) and a next pointer to the following node—or null at the tail. You keep a head reference to the first node, prepend in O(1) by rewiring head, or walk forward with a cursor when you need ordered inserts at the end. JavaScript has no built-in linked list in the standard library; you model nodes with plain objects or classes. Arrays cover most ordered collections in apps—linked lists stay relevant for interviews, puzzles, and APIs shaped like chains.

Environment: Node.js v20.18.2. Snippets are self-contained; the note after each block states the expected console output.


ListNode shape (javascript listnode)

Diagram: singly linked nodes with val and next fields chained from head to tail

A minimal singly linked pair: the head points at the second node; the tail’s next is null.

javascript
const tail = { val: 1, next: null };
const head = { val: 2, next: tail };
console.log(`${head.val}->${tail.val}`);
Output

You should see one line logging 2->1.

val matches common listnode examples; you can rename it to data everywhere if you prefer.


How to create a linked list in javascript (forward order)

Build [1,2,3,4,5] in one forward pass by hanging each new node off the previous tail.

javascript
function listFromArrayForward(arr) {
  if (arr.length === 0) {
    return null;
  }
  const h = { val: arr[0], next: null };
  let t = h;
  for (let i = 1; i < arr.length; i++) {
    t.next = { val: arr[i], next: null };
    t = t.next;
  }
  return h;
}

const head = listFromArrayForward([1, 2, 3, 4, 5]);
for (let cur = head; cur; cur = cur.next) {
  console.log(cur.val);
}
Output

You should see 5 lines, in order: 1, 2, 3, 4, 5.


Prepend by scanning the array backward

Another classic pattern: walk the array from the end toward the start and prepend each value at the head. You still get forward order when you traverse from head.

javascript
function createLinkedList(dataArray) {
  let h = null;
  for (let i = dataArray.length - 1; i >= 0; i--) {
    h = { val: dataArray[i], next: h };
  }
  return h;
}

const head = createLinkedList([1, 2, 3, 4, 5]);
for (let cur = head; cur; cur = cur.next) {
  console.log(cur.val);
}
Output

You should see 5 lines, in order: 1, 2, 3, 4, 5.


LinkedList class: prepend add and remove

Diagram: before and after rewiring next pointers when removing a middle node from a singly linked list

A small class keeps this.head and mutates links when removing the first node whose val matches.

javascript
class LinkedList {
  constructor() {
    this.head = null;
  }

  add(val) {
    this.head = { val, next: this.head };
  }

  remove(val) {
    let cur = this.head;
    let prev = null;
    while (cur) {
      if (cur.val === val) {
        if (prev) {
          prev.next = cur.next;
        } else {
          this.head = cur.next;
        }
        return true;
      }
      prev = cur;
      cur = cur.next;
    }
    return false;
  }
}

function vals(h) {
  const a = [];
  for (let n = h; n; n = n.next) {
    a.push(n.val);
  }
  return a;
}

const list = new LinkedList();
list.add("node1 data");
list.add("node2 data");
list.add("node3 data");
list.remove("node2 data");
console.log(JSON.stringify(vals(list.head)));
Output

You should see one line logging ["node3 data","node1 data"].

add prepends, so the first string you pass ends up at the far end of the chain; remove splices out the middle link and leaves node3 datanode1 data.


Summary

Linked list javascript and linked list js tutorials should leave you with one mental model: nodes are { val, next } (or data if you rename the field), and algorithms revolve around keeping a correct head pointer while you prepend, append, or splice. People ask how to create a linked list in javascript without a standard library class—either build forward with a tail cursor, or scan an array backward while prepending at the head, both of which preserve order when you traverse from head.

Interview searches for listnode js or javascript listnode are the same structure under LeetCode naming. In production JavaScript you still reach for arrays for cache locality and random access, but understanding singly linked lists matters for exercises, custom queues, and APIs that already expose chain-shaped data—just be disciplined about null checks and pointer order when you delete a middle node.


References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …