Types of Linked Lists

Types of Linked Lists

Key Takeaways: The standard Linked List only allows you to move forward. To solve different types of problems, Computer Scientists created Doubly Linked Lists (which move backward too) and Circular Linked Lists (which loop infinitely).

Not all Linked Lists are created equal. Depending on the problem you are trying to solve, you might need a different type of chain.

Let's explore the three most common types of Linked Lists in Data Structures and Algorithms!


1. Singly Linked List

The Singly Linked List is the most basic version. This is the one we have been looking at in previous tutorials.

Pros & Cons


2. Doubly Linked List

The Doubly Linked List fixes the main weakness of the Singly Linked List by giving you the ability to walk backwards.

Real-Life Analogy

Think of your web browser's history. You can click the "Forward" button to go to the next page, but you can also click the "Back" button to return to the previous page instantly.

Pros & Cons


3. Circular Linked List

The Circular Linked List is a chain that never ends.

(Note: You can also create a Doubly Circular Linked List, where both the Next and Prev pointers loop around!)

Real-Life Analogy

Think of a multiplayer board game like Monopoly. Player 1 takes a turn, then Player 2, then Player 3, then Player 4. Who goes after Player 4? It loops right back to Player 1!


Visualizing the Differences

Here is a quick visual reference for how these three structures differ in memory:

Visual representation of Singly, Doubly, and Circular Linked Lists

The 3 Types: Singly (Forward), Doubly (Both ways), and Circular (Infinite Loop).


Coding a Doubly Linked List Node

Let's look at how the code changes when we upgrade from a Singly Linked List to a Doubly Linked List in Python.

Notice how we just add a self.prev property to the Node class!

Doubly Linked List Node

class DoublyNode:
    def __init__(self, data):
        self.data = data
        self.next = None  # Points forward
        self.prev = None  # Points backward
# 1. Create Nodes
node1 = DoublyNode("A")
node2 = DoublyNode("B")
# 2. Link them Forward AND Backward!
node1.next = node2
node2.prev = node1

print("Forward link from node1:", node1.next.data) print("Backward link from node2:", node2.prev.data)


Exercise 1 of 1

?

Which type of Linked List would be best for creating a music player queue that repeats forever?