0
0
DSA Pythonprogramming

Creating a Singly Linked List from Scratch in DSA Python - Step-by-Step

Choose your learning style9 modes available
Mental Model
A singly linked list is a chain of boxes where each box holds a value and points to the next box in line.
Analogy: Imagine a treasure hunt where each clue leads you to the next clue. Each clue is like a box holding a value and a pointer to the next clue.
head -> [value | next] -> [value | next] -> [value | null]
↑
Dry Run Walkthrough
Input: Create a singly linked list with values 1, 2, 3 in order
Goal: Build a linked list where each node points to the next, ending with null
Step 1: Create first node with value 1 and set it as head
head -> [1 | null]
↑
Why: We need a starting point for the list
Step 2: Create second node with value 2 and link it after first node
head -> [1 | next] -> [2 | null]
Why: Link second node so list continues after first
Step 3: Create third node with value 3 and link it after second node
head -> [1 | next] -> [2 | next] -> [3 | null]
Why: Link third node to complete the chain
Result:
head -> 1 -> 2 -> 3 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

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

    def append(self, value):
        new_node = Node(value)  # create new node
        if not self.head:
            self.head = new_node  # set head if list empty
            return
        curr = self.head
        while curr.next:
            curr = curr.next  # move to last node
        curr.next = new_node  # link new node at end

    def __str__(self):
        values = []
        curr = self.head
        while curr:
            values.append(str(curr.value))
            curr = curr.next  # move to next node
        return ' -> '.join(values) + ' -> null'

# Driver code
linked_list = SinglyLinkedList()
linked_list.append(1)
linked_list.append(2)
linked_list.append(3)
print(linked_list)
new_node = Node(value) # create new node
create a new box with the given value
if not self.head: self.head = new_node # set head if list empty return
if list is empty, start it with new node
while curr.next: curr = curr.next # move to last node
walk through nodes until last one
curr.next = new_node # link new node at end
attach new node after last node
while curr: values.append(str(curr.value)) curr = curr.next # move to next node
collect all node values to print list
OutputSuccess
1 -> 2 -> 3 -> null
Complexity Analysis
Time: O(n) because to add a node at the end, we may need to walk through all n nodes
Space: O(n) because we store n nodes in memory
vs Alternative: Compared to arrays, linked lists allow easy insertion without shifting elements but require walking nodes to find the end
Edge Cases
Empty list when appending first node
The new node becomes the head of the list
DSA Python
if not self.head:
    self.head = new_node  # set head if list empty
When to Use This Pattern
When you need a flexible list that grows by linking nodes instead of fixed-size arrays, use singly linked list creation.
Common Mistakes
Mistake: Not setting the head when the list is empty
Fix: Check if head is None and assign new node as head before traversing
Summary
Creates a chain of nodes where each node points to the next, forming a singly linked list.
Use when you want a list structure that can grow dynamically without resizing arrays.
The key is to keep track of the head and link each new node at the end by updating pointers.