0
0
Data Structures Theoryknowledge~6 mins

Singly linked list structure in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a chain of paper clips linked one after another. You want to keep track of each clip and the next one in line, but you don't want to hold the whole chain at once. This is the problem a singly linked list solves: it helps organize items in a sequence where each item knows only about the next one.
Explanation
Node
A singly linked list is made up of nodes. Each node holds two things: the data (the actual value or information) and a reference (or pointer) to the next node in the list. This setup allows the list to grow or shrink easily by changing these references.
Each node stores data and a link to the next node, forming the chain.
Head
The head is the first node in the list. It acts as the starting point to access all other nodes. Without the head, you cannot reach the rest of the list because nodes only know about their immediate next node.
The head node is the entry point to the entire list.
Next Pointer
Each node has a next pointer that points to the following node in the list. If a node's next pointer is empty or null, it means that node is the last one in the list. This pointer creates the link between nodes.
The next pointer connects nodes in one direction, from first to last.
Traversal
To access or find a node, you start at the head and follow each next pointer until you reach the desired node or the end of the list. This process is called traversal and it moves step-by-step through the list.
Traversal means moving through the list by following next pointers from the head.
Dynamic Size
Unlike arrays, singly linked lists can easily grow or shrink because nodes can be added or removed by changing pointers. This makes them flexible for situations where the number of items changes often.
Singly linked lists can change size easily by updating node links.
Real World Analogy

Imagine a treasure hunt where each clue leads you to the next one. You start with the first clue, and each clue only tells you where to find the next clue, not the whole path. You follow the clues one by one until you reach the treasure.

Node → Each clue containing a hint and the location of the next clue
Head → The very first clue that starts the treasure hunt
Next Pointer → The direction or location given in each clue to find the next clue
Traversal → Following each clue step-by-step to reach the treasure
Dynamic Size → Adding or removing clues easily without changing the whole hunt
Diagram
Diagram
┌───────┐     ┌───────┐     ┌───────┐     ┌───────┐
│ Data  │ --> │ Data  │ --> │ Data  │ --> │  null │
│ Next  │     │ Next  │     │ Next  │     │       │
└───────┘     └───────┘     └───────┘     └───────┘
  Head
This diagram shows a chain of nodes where each node points to the next, starting from the head and ending with null.
Key Facts
NodeA unit in a singly linked list containing data and a pointer to the next node.
HeadThe first node in a singly linked list that serves as the starting point.
Next PointerA reference in a node that points to the next node in the list.
TraversalThe process of visiting nodes one by one starting from the head.
Null PointerA special pointer indicating the end of the list with no next node.
Code Example
Data Structures Theory
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 traverse(self):
        current = self.head
        while current:
            print(current.data)
            current = current.next

# Example usage
sll = SinglyLinkedList()
sll.append(10)
sll.append(20)
sll.append(30)
sll.traverse()
OutputSuccess
Common Confusions
Believing nodes know about previous nodes in the list.
Believing nodes know about previous nodes in the list. In a singly linked list, nodes only know about the next node, not the previous one.
Thinking the list can be accessed randomly like an array.
Thinking the list can be accessed randomly like an array. Accessing nodes requires traversal from the head; random access is not possible.
Assuming the last node points back to the head or another node.
Assuming the last node points back to the head or another node. The last node's next pointer is always null, marking the list's end.
Summary
A singly linked list is a chain of nodes where each node points only to the next one.
The head node is the starting point to access the entire list.
Nodes can be added or removed easily by changing the next pointers, making the list flexible.