Foundation·Concept·2 min read

Linked list in JavaScript

Practice this question

Node structure

JavaScript
class ListNode {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

Problems to practice

  • Reverse Linked List
  • Middle of Linked List
  • Detect Cycle
  • Remove Nth Node from End
  • Merge Two Sorted Lists
  • Intersection of Two Linked Lists

Example: reverse linked list

JavaScript
function reverseList(head) {
  let prev = null;
  let curr = head;

  while (curr) {
    const next = curr.next;
    curr.next = prev;
    prev = curr;
    curr = next;
  }

  return prev;
}

Adapted from 16_DSA_JAVASCRIPT.md · “4. Linked list in JavaScript”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.