0
0
Data-structures-theoryConceptBeginner · 3 min read

What is a Singly Linked List: Definition and Examples

A singly linked list is a data structure made of nodes where each node holds data and a reference to the next node only. It forms a chain-like sequence allowing efficient insertion and deletion from the front but only one-way traversal.
⚙️

How It Works

Imagine a chain of paperclips where each paperclip holds a small note and is linked only to the next paperclip in line. In a singly linked list, each node is like a paperclip holding some data and a pointer to the next node. You can only move forward from one node to the next, never backward.

This structure allows you to add or remove nodes easily at the start or middle by changing the links, without shifting all the data like in an array. However, to find a node, you must start at the beginning and follow the links one by one until you reach it.

💻

Example

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

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

class SinglyLinkedList:
    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, end=' -> ' if current.next else '')
            current = current.next

# Create list and add nodes
linked_list = SinglyLinkedList()
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)

# Print list
linked_list.print_list()
Output
10 -> 20 -> 30
🎯

When to Use

Use a singly linked list when you need a simple, dynamic collection that grows or shrinks often, especially when you add or remove items mostly at the front or middle. It is useful when memory efficiency matters because it only stores one link per node.

Common real-world uses include implementing stacks, queues, or managing playlists where you move forward through items. However, if you need fast random access or backward traversal, other structures like arrays or doubly linked lists are better.

Key Points

  • A singly linked list consists of nodes linked in one direction only.
  • Each node contains data and a reference to the next node.
  • Efficient for insertions and deletions at the start or middle.
  • Traversal is one-way, from head to end.
  • Not suitable for fast random access or backward navigation.

Key Takeaways

A singly linked list is a chain of nodes linked one-way, each holding data and a next pointer.
It allows easy insertion and deletion without shifting elements like arrays.
Traversal is only forward, so finding elements requires starting from the head.
Ideal for dynamic collections with frequent changes at the front or middle.
Not suitable when you need fast random access or backward traversal.