Data Structure and Algorithm
Data Structure
Linked List
Singly Linked List
Linked List Implementation

Simple Linked List Implementation in JavaScript


What is a Linked List?

A Linked List is a linear data structure made up of nodes.
Each node has two parts:

  1. Data → stores the actual value.
  2. Next Pointer → points to the next node in the list.

The first node is called the head.
If the list is empty, the head is NULL.


Simple Example in JavaScript

Here’s a basic implementation of a linked list with three nodes:

// Define a Node class
class Node {
    constructor(value) {
        this.data = value;   // store the value
        this.next = null;    // pointer to the next node
    }
}
 
// Create nodes
let head = new Node(5);       // first node (head)
let second = new Node(15);    // second node
let third = new Node(25);     // third node
 
// Link nodes together
head.next = second;
second.next = third;
 
// Function to print the linked list
function printList(node) {
    let current = node;
    let result = "";
    while (current !== null) {
        result += current.data + " --> ";
        current = current.next;
    }
    result += "NULL";  // marks the end of the list
    console.log(result);
}
 
// Print the linked list
printList(head);

Output

5 --> 15 --> 25 --> NULL

How It Works (Easy Explanation)

  • Each Node stores a value (data) and a link to the next node (next).
  • We created three nodes with values 5, 15, 25.
  • We connected them using the next pointer.
  • The printList function starts from the head and keeps following the next pointer until the end (NULL).

So the linked list looks like this:

5 → 15 → 25 → NULL