0
0
Data-structures-theoryHow-ToBeginner ยท 4 min read

Types of Linked List: Simple, Doubly, and Circular Explained

There are three main types of linked lists: singly linked list, where each node points to the next node; doubly linked list, where nodes point to both next and previous nodes; and circular linked list, where the last node links back to the first node, forming a circle.
๐Ÿ“

Syntax

A linked list is made of nodes. Each node contains data and links (pointers) to other nodes.

  • Singly Linked List: Each node has data and a pointer to the next node.
  • Doubly Linked List: Each node has data, a pointer to the next node, and a pointer to the previous node.
  • Circular Linked List: The last node points back to the first node, making a loop.
javascript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null; // points to next node
        this.prev = null; // used only in doubly linked list
    }
}

// Singly Linked List node example
const node1 = new Node(10);
const node2 = new Node(20);
node1.next = node2;

// Doubly Linked List node example
node2.prev = node1;

// Circular Linked List example
node2.next = node1; // last node points back to first node
๐Ÿ’ป

Example

This example shows how to create a simple singly linked list with three nodes and print their values.

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

class SinglyLinkedList {
    constructor() {
        this.head = null;
    }

    append(data) {
        const newNode = new Node(data);
        if (!this.head) {
            this.head = newNode;
            return;
        }
        let current = this.head;
        while (current.next) {
            current = current.next;
        }
        current.next = newNode;
    }

    printList() {
        let current = this.head;
        let result = '';
        while (current) {
            result += current.data + ' -> ';
            current = current.next;
        }
        result += 'null';
        console.log(result);
    }
}

const list = new SinglyLinkedList();
list.append(1);
list.append(2);
list.append(3);
list.printList();
Output
1 -> 2 -> 3 -> null
โš ๏ธ

Common Pitfalls

Common mistakes when working with linked lists include:

  • Forgetting to update pointers when inserting or deleting nodes, which can break the list.
  • Not handling the null or None case for the last node, causing errors.
  • In circular linked lists, accidentally creating infinite loops when traversing without a proper stop condition.
  • In doubly linked lists, forgetting to update both next and prev pointers.
javascript
/* Wrong: Inserting node without updating prev pointer in doubly linked list */
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
        this.prev = null;
    }
}

// Correct insertion updates both next and prev pointers
function insertAfter(node, newNode) {
    newNode.next = node.next;
    if (node.next) {
        node.next.prev = newNode;
    }
    node.next = newNode;
    newNode.prev = node;
}
๐Ÿ“Š

Quick Reference

TypeDescriptionPointer(s) per NodeUse Case
Singly Linked ListNodes point only to the next node1 (next)Simple lists, less memory usage
Doubly Linked ListNodes point to next and previous nodes2 (next, prev)Easy backward traversal, complex operations
Circular Linked ListLast node points back to first node1 or 2 (next, optional prev)Continuous looping, round-robin tasks
โœ…

Key Takeaways

Singly linked lists have nodes pointing only forward to the next node.
Doubly linked lists allow traversal in both directions with next and previous pointers.
Circular linked lists connect the last node back to the first, forming a loop.
Always update all relevant pointers when inserting or deleting nodes to avoid breaking the list.
Be careful with traversal in circular lists to prevent infinite loops.