0
0
JavascriptHow-ToBeginner · 3 min read

How to Reverse a Linked List in JavaScript: Simple Guide

To reverse a linked list in JavaScript, iterate through the list while changing each node's next pointer to the previous node. Use three pointers: prev, current, and next to track nodes during reversal.
📐

Syntax

The basic syntax to reverse a singly linked list involves a loop that updates pointers:

  • prev: Tracks the previous node, starts as null.
  • current: Tracks the current node being processed.
  • next: Temporarily stores the next node before changing links.

Inside the loop, set current.next = prev to reverse the link, then move prev and current forward.

javascript
function reverseLinkedList(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    let next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}
💻

Example

This example creates a simple linked list, reverses it, and prints the values before and after reversal.

javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

function printList(head) {
  let current = head;
  let values = [];
  while (current !== null) {
    values.push(current.value);
    current = current.next;
  }
  console.log(values.join(' -> '));
}

function reverseLinkedList(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    let next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}

// Create linked list: 1 -> 2 -> 3 -> 4
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);

console.log('Original list:');
printList(head);

const reversedHead = reverseLinkedList(head);
console.log('Reversed list:');
printList(reversedHead);
Output
Original list: 1 -> 2 -> 3 -> 4 Reversed list: 4 -> 3 -> 2 -> 1
⚠️

Common Pitfalls

Common mistakes when reversing a linked list include:

  • Not saving the next node before changing current.next, which breaks the chain.
  • Forgetting to update prev and current pointers properly, causing infinite loops or lost nodes.
  • Returning the wrong node; the new head is prev after the loop ends.
javascript
function wrongReverse(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    // Forgot to save next node
    current.next = prev;
    prev = current;
    current = current.next; // This causes current to be prev, leading to infinite loop
  }
  return prev;
}

// Correct way saves next before changing links:
function correctReverse(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    let next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}
📊

Quick Reference

Tips for reversing a linked list:

  • Always save the next node before changing links.
  • Use three pointers: prev, current, and next.
  • Return prev as the new head after reversal.
  • Test with small lists to verify correctness.

Key Takeaways

Use three pointers to reverse links safely: prev, current, and next.
Always save the next node before changing the current node's next pointer.
Return the prev pointer as the new head after the loop finishes.
Common errors include losing track of nodes or infinite loops from wrong pointer updates.