Singly Linked List Explained in the Simplest Way

Understand how singly linked lists work with easy diagrams, analogies, and code examples. Perfect for beginners.

Singly Linked List Explained in the Simplest Way

If you're just starting out with data structures, singly linked lists might seem confusing at first. But don't worry — this guide will break it down into plain English, with real-life examples and simple code.


🧠 What Is a Singly Linked List?

A singly linked list is a data structure that stores a sequence of elements. Each element (called a node) contains:

  • A value
  • A pointer (or reference) to the next node in the list

It's like a chain — each link knows where the next one is.


🧩 Visual Representation

[10 | ➡] → [20 | ➡] → [30 | ➡] → null
  • Each box is a node
  • 10, 20, 30 are the values
  • means a pointer to the next node
  • null means the end of the list

🪢 Real-Life Analogy

Think of a conga line at a party.
Each dancer holds the shoulder of the person in front of them (just one person) — that's a singly linked list!


✏️ Node Structure in Code (JavaScript Example)

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

🏗 Creating a Simple Linked List

const node1 = new Node(10);
const node2 = new Node(20);
const node3 = new Node(30);

// Linking the nodes
node1.next = node2;
node2.next = node3;

// node1 is now the head of the linked list

🔄 How Do You Traverse a Linked List?

You start at the head and follow the next pointers.

let current = node1;

while (current !== null) {
  console.log(current.value);
  current = current.next;
}

📤 Output:

10
20
30

⚙️ Common Operations

Operation Description
Insertion Add a node at the beginning or end
Deletion Remove a node by value or position
Traversal Go through each node one-by-one
Search Find a node with a specific value

⏱ Time and Space Complexity

Operation Time Complexity Space Complexity
Insert at Head O(1) O(1)
Insert at Tail O(n) O(1)
Delete Node O(n) O(1)
Search O(n) O(1)
Traverse O(n) O(1)
  • n is the number of nodes in the list.
  • Linked list uses less memory compared to arrays when resizing, but has more overhead due to pointers.

🧪 Real-Life Example: 🎵 Super‑Simple Music Player with Singly Linked List

1. The Idea in One Sentence

A playlist is just a chain of songs where each song knows only the next song.
That chain is called a singly linked list.

2. Full Code (JavaScript)

/* music-player.js */

// Box that stores one song
class Node {
  constructor(song) {
    this.song = song;   // song title (string)
    this.next = null;   // pointer to the next box
  }
}

// Playlist managed as a singly linked list
class MusicPlayer {
  constructor() {
    this.head = null;   // first song in the list
  }

  // Add song at the end
  insertAtEnd(song) {
    const node = new Node(song);
    if (!this.head) {           // empty list
      this.head = node;
      return;
    }
    let curr = this.head;       // walk to the tail
    while (curr.next) curr = curr.next;
    curr.next = node;           // link new tail
  }

  // Add song at the beginning
  insertAtBeginning(song) {
    const node = new Node(song);
    node.next = this.head;
    this.head = node;
  }

  // Remove song by title
  deleteByValue(title) {
    if (!this.head) return;
    if (this.head.song === title) {       // first box
      this.head = this.head.next;
      return;
    }
    let curr = this.head;
    while (curr.next && curr.next.song !== title) curr = curr.next;
    if (curr.next) curr.next = curr.next.next;  // skip the box
  }

  // Check if a song exists
  search(title) {
    let curr = this.head;
    while (curr) {
      if (curr.song === title) return true;
      curr = curr.next;
    }
    return false;
  }

  // Print all songs
  traverse() {
    let curr = this.head;
    while (curr) {
      console.log("🎵", curr.song);
      curr = curr.next;
    }
  }
}

/* ----- Tiny Demo ----- */
const player = new MusicPlayer();
player.insertAtEnd("Song A");
player.insertAtEnd("Song B");
console.log("Initial playlist:");
player.traverse();                      // 🎵 Song A  🎵 Song B

player.deleteByValue("Song A");
console.log("\nAfter removing 'Song A':");
player.traverse();                      // 🎵 Song B

console.log("\nIs 'Song B' there?", player.search("Song B")); // true

Try it: Save as music-player.js and run node music-player.js.

3. How Each Operation Works

Operation What we do Steps Inside
Add at Front Put a new song first 1. Point new song to current first.
2. Make new song the new head.
Add at End Put a new song last 1. Walk until you reach the tail (next === null).
2. Link tail to new song.
Delete by Title Remove a specific song 1. If it’s the first song, just move head.
2. Otherwise walk until the song before the one to delete and skip it.
Search See if a song exists Start at the head and read each title until found or end reached.
Traverse List every song Start at the head and follow next printing each title.

All these actions share one move: walk the chain by following next links.

4. Quick Analogy

Think of beads on a string: each bead is a song, and the string tells you where the next bead is.
You can add a bead at the start or end, remove one, or slide your fingers along to see every bead.

5. Complexity (Quick View)

Action Time Extra Space
Insert front O(1) O(1)
Insert end O(n) O(1)
Delete/Search O(n) O(1)
Traverse O(n) O(1)

(n = number of songs)


✅ When to Use Singly Linked List?

  • When you need dynamic memory allocation
  • When you don’t know the final size of your data
  • When you frequently insert/remove items from the start

⚠️ Limitations

  • Slower access time (no direct index like arrays)
  • Can't traverse backwards (it's singly linked)

📌 Summary

  • A singly linked list is a chain of nodes.
  • Each node has a value and a pointer to the next.
  • It’s great for insertions/deletions but not for random access.

🧭 What’s Next?

Coming up:
Doubly Linked List Explained Simply

Happy coding! 🧑‍💻