0
0
DSA Pythonprogramming

Get Length of Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
Count each node one by one until you reach the end of the list.
Analogy: Like counting people standing in a line by moving from the first person to the last one.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Find how many nodes are in the linked list
Step 1: start at head node with value 1
head -> [curr->1] -> 2 -> 3 -> null
Why: We begin counting from the first node
Step 2: count node 1 and move curr to next node
head -> 1 -> [curr->2] -> 3 -> null
Why: Counted first node, move to second to continue counting
Step 3: count node 2 and move curr to next node
head -> 1 -> 2 -> [curr->3] -> null
Why: Counted second node, move to third node
Step 4: count node 3 and move curr to null (end)
head -> 1 -> 2 -> 3 -> [curr->null]
Why: Counted last node, reached end of list
Result:
head -> 1 -> 2 -> 3 -> null
Length = 3
Annotated Code
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

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

    def get_length(self):
        count = 0
        curr = self.head
        while curr:
            count += 1  # count current node
            curr = curr.next  # move to next node
        return count

# Driver code
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
length = ll.get_length()
print(f"Length = {length}")
while curr:
loop through each node until end of list
count += 1 # count current node
increment count for each node visited
curr = curr.next # move to next node
advance pointer to next node to continue counting
OutputSuccess
Length = 3
Complexity Analysis
Time: O(n) because we visit each node once to count
Space: O(1) because we only use a few variables for counting
vs Alternative: Alternative would be storing length in a variable updated on insert/delete, which uses extra space but gives O(1) length retrieval
Edge Cases
empty list
returns length 0 because head is None and loop never runs
DSA Python
while curr:
single node list
returns length 1 by counting the single node
DSA Python
while curr:
When to Use This Pattern
When you need to know how many elements are in a linked list, use a simple traversal counting each node until the end.
Common Mistakes
Mistake: Forgetting to move the current pointer to the next node inside the loop
Fix: Add 'curr = curr.next' inside the while loop to advance the pointer
Mistake: Initializing count to 1 instead of 0, causing off-by-one error
Fix: Initialize count to 0 before starting the loop
Summary
Counts the number of nodes in a linked list by visiting each node once.
Use when you need to find the size of a linked list without stored length.
The key is to start at the head and move through each node until you reach the end.