0
0
DSA Pythonprogramming~10 mins

Creating a Singly Linked List from Scratch in DSA Python - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating a Singly Linked List from Scratch
Start
Create Node with data
Is list empty?
Yes No
Set head to new node
Set last node.next to new node
Done
This flow shows how to create a new node and add it to the singly linked list, either as the first node or at the end.
Execution Sample
DSA 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
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
This code creates a singly linked list and appends three nodes with data 1, 2, and 3.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10Nonenull
2List empty? Yes, set head to new node1head -> Node(1)1 -> null
3Create new node with data=21None1 -> null
4List empty? No, traverse to last node1current -> Node(1)1 -> null
5Set last node.next to new node2Node(1).next -> Node(2)1 -> 2 -> null
6Create new node with data=32None1 -> 2 -> null
7List empty? No, traverse to last node2current -> Node(1)1 -> 2 -> null
8Move current to next node2current -> Node(2)1 -> 2 -> null
9Set last node.next to new node3Node(2).next -> Node(3)1 -> 2 -> 3 -> null
10End of append operations3No pointer changes1 -> 2 -> 3 -> null
💡 All nodes appended; list now has 3 nodes with last node.next = None
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 6After Step 9Final
headNoneNoneNode(1)Node(1)Node(1)Node(1)Node(1)Node(1)
currentNoneNoneNoneNode(1)Node(1)NoneNode(2)None
new_nodeNoneNode(1)NoneNode(2)NoneNode(3)NoneNone
list size00112233
Key Moments - 3 Insights
Why do we check if the list is empty before adding a new node?
Because if the list is empty (head is None), we must set head to the new node directly. This is shown in execution_table step 2 where head changes from None to Node(1).
Why do we traverse the list to find the last node before adding a new node?
Because in a singly linked list, we can only add a new node at the end by finding the last node whose next is None. This traversal is shown in steps 4, 7, and 8.
What does the 'next' pointer of the last node point to after appending?
It points to the new node added. For example, in step 5, Node(1).next points to Node(2), and in step 9, Node(2).next points to Node(3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the visual state after step 5?
A2 -> 1 -> null
B1 -> null
C1 -> 2 -> null
Dnull
💡 Hint
Check the 'Visual State' column in row for step 5.
At which step does the head pointer first get assigned a node?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Pointer Changes' column to see when head changes from None.
If we skip the traversal loop, what would happen when appending the second node?
AThe new node would be added after the last node correctly
BThe new node would become head, losing the first node
CThe list would remain unchanged
DAn error would occur
💡 Hint
For the second node, current is set to head (which is the last node), and since head.next is None, the loop doesn't run anyway, so skipping it adds correctly.
Concept Snapshot
Creating a singly linked list:
- Define Node with data and next pointer
- Start with head = None
- To append: create new node
- If list empty, set head to new node
- Else traverse to last node
- Set last node.next to new node
- List ends with last node.next = None
Full Transcript
We start by creating a Node class with data and next pointer. The LinkedList class has a head pointer initially None. When appending, we create a new node. If the list is empty, we set head to this new node. Otherwise, we traverse from head to find the last node (where next is None). Then we set last node's next pointer to the new node. This process is repeated for each append. The execution table shows each step, pointer changes, and the visual linked list state. The variable tracker shows how head, current, new_node, and list size change over time. Key moments clarify why we check if the list is empty and why traversal is needed. The visual quiz tests understanding of these steps. The concept snapshot summarizes the process in simple steps.