0
0
Data-structures-theoryConceptBeginner · 3 min read

What is Linked List: Definition, Example, and Uses

A linked list is a data structure made of nodes where each node holds data and a reference to the next node. It allows dynamic memory use and easy insertion or removal of elements without shifting others.
⚙️

How It Works

Imagine a linked list as a chain of paper clips connected one after another. Each paper clip represents a node that holds some information and a link to the next paper clip in the chain. This way, you can follow the chain from the first paper clip to the last.

Unlike an array where elements are stored in fixed positions, a linked list stores elements scattered in memory but connected by links. This makes it easy to add or remove paper clips anywhere in the chain without moving the others.

💻

Example

This example shows a simple linked list in Python with three nodes holding numbers. It prints each number by following the links.

python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node

    def print_list(self):
        current = self.head
        while current:
            print(current.data)
            current = current.next

# Create linked list and add elements
ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)

ll.print_list()
Output
10 20 30
🎯

When to Use

Use a linked list when you need a flexible way to store items that can grow or shrink often. It is helpful when you want to insert or delete items quickly without reorganizing the whole collection.

Real-world uses include managing playlists, undo functionality in apps, or any situation where you want to add or remove items frequently and in any order.

Key Points

  • A linked list is made of nodes connected by links.
  • It allows easy insertion and deletion without shifting elements.
  • Nodes store data and a reference to the next node.
  • It uses memory dynamically, unlike arrays.
  • Good for applications needing frequent changes in data size.

Key Takeaways

A linked list stores data in nodes connected by links, allowing flexible memory use.
It is ideal for situations where you add or remove items often and quickly.
Each node contains data and a reference to the next node, forming a chain.
Linked lists avoid the need to shift elements like in arrays during insertions or deletions.
Common uses include playlists, undo features, and dynamic data collections.