Introduction
Linked lists are one of the fundamental data structures used in computer science. They are dynamic, allowing efficient insertions and deletions. In this post, we’ll explore two types: Singly Linked Lists and Doubly Linked Lists, with real-life examples and JavaScript implementations.
1️⃣ Singly Linked List
A Singly Linked List (SLL) consists of nodes where each node contains:
- A value (data)
- A next pointer pointing to the next node
However, it does not have a reference to the previous node, so traversal is one-directional.
🔹 Real-Life Example
Train with one-way connectivity
Imagine a train where each coach is connected only to the next one. You can move forward but not backward.
Other examples:
- Music Playlist (Next song only)
- Call Logs on Phones
🔹 JavaScript Implementation
class Node { constructor(value) { this.value = value; this.next = null; } } class SinglyLinkedList { constructor() { this.head = null; } // Insert at the end append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } // Print the list print() { let current = this.head; let list = ""; while (current) { list += current.value + " -> "; current = current.next; } console.log(list + "null"); } } // Example usage: const sll = new SinglyLinkedList(); sll.append(10); sll.append(20); sll.append(30); sll.print(); // Output: 10 -> 20 -> 30 -> null
2️⃣ Doubly Linked List
A Doubly Linked List (DLL) consists of nodes where each node contains:
- A value (data)
- A next pointer pointing to the next node
- A previous pointer pointing to the previous node
This allows traversal in both directions.
🔹 Real-Life Example
Web Browser History
When you navigate in a browser, you can go forward and backward smoothly because each page is linked to both the previous and next page.
Other examples:
- Music Playlist (Next & Previous buttons)
- Undo/Redo in Software
🔹 JavaScript Implementation
class DNode { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } // Insert at the end append(value) { const newNode = new DNode(value); if (!this.head) { this.head = this.tail = newNode; return; } this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } // Print forward printForward() { let current = this.head; let list = ""; while (current) { list += current.value + " <-> "; current = current.next; } console.log(list + "null"); } // Print backward printBackward() { let current = this.tail; let list = ""; while (current) { list += current.value + " <-> "; current = current.prev; } console.log(list + "null"); } } // Example usage: const dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.printForward(); // Output: 10 <-> 20 <-> 30 <-> null dll.printBackward(); // Output: 30 <-> 20 <-> 10 <-> null
🚀 Key Differences
| Feature | Singly Linked List | Doubly Linked List |
|----------------------|------------------|------------------|
| Direction | Forward only | Forward & Backward |
| Memory Usage | Less (1 pointer) | More (2 pointers) |
| Traversal | One-way | Two-way |
| Reverse Traversal | ❌ Not possible | ✅ Possible |
| Use Case Examples | Train (One-way), Call logs | Browser history, Undo/Redo |
Conclusion
- Singly Linked Lists are more memory-efficient and simpler.
- Doubly Linked Lists offer more flexibility for traversing in both directions.
- Choosing the right linked list depends on the specific use case.
Hope this guide helps you understand linked lists better! 🚀 Feel free to drop any questions or suggestions below. Happy coding! 💻
Top comments (0)