0
0
DSA Pythonprogramming~10 mins

Get Length of Linked List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Get Length of Linked List
Start at head node
Is current node None?
YesStop, length counted
No
Increment length count
Move to next node
Back to check current node
Start from the head node, move through each node until the end, counting nodes one by one.
Execution Sample
DSA Python
def get_length(head):
    length = 0
    current = head
    while current is not None:
        length += 1
        current = current.next
    return length
Counts how many nodes are in the linked list by moving from head to end.
Execution Table
StepOperationCurrent NodeLength CountVisual State
1Start at headNode(1)01 -> 2 -> 3 -> None
2Check current != NoneNode(1)01 -> 2 -> 3 -> None
3Increment lengthNode(1)11 -> 2 -> 3 -> None
4Move to next nodeNode(2)11 -> 2 -> 3 -> None
5Check current != NoneNode(2)11 -> 2 -> 3 -> None
6Increment lengthNode(2)21 -> 2 -> 3 -> None
7Move to next nodeNode(3)21 -> 2 -> 3 -> None
8Check current != NoneNode(3)21 -> 2 -> 3 -> None
9Increment lengthNode(3)31 -> 2 -> 3 -> None
10Move to next nodeNone31 -> 2 -> 3 -> None
11Check current != NoneNone31 -> 2 -> 3 -> None
12Stop and return lengthNone31 -> 2 -> 3 -> None
💡 Current node is None, end of list reached, length counted as 3
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9Final
length01233
currentNode(1)Node(2)Node(3)NoneNone
Key Moments - 3 Insights
Why do we check if current is None before counting?
Because when current is None, it means we reached the end of the list and should stop counting. See execution_table steps 2, 5, 8, and 11.
Why do we move current to current.next after incrementing length?
We count the current node first, then move to the next node to continue counting. This order ensures no node is skipped. See execution_table steps 3 and 4.
What happens if the list is empty (head is None)?
The loop never runs because current is None at start, so length stays 0. See execution_table step 11 where current is None and loop stops immediately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the length count after step 6?
A1
B3
C2
D0
💡 Hint
Check the 'Length Count' column at step 6 in the execution_table.
At which step does the current node become None?
AStep 9
BStep 10
CStep 11
DStep 12
💡 Hint
Look at the 'Current Node' column to find when it changes to None.
If the linked list had 5 nodes instead of 3, how many times would length increment?
A5
B4
C3
D6
💡 Hint
Length increments once per node, so check variable_tracker for length increments.
Concept Snapshot
Get Length of Linked List:
- Start at head node
- While current node is not None:
  - Increase count by 1
  - Move to next node
- Return count as length
Simple traversal counting nodes until end.
Full Transcript
To get the length of a linked list, start at the head node and move through each node one by one. For each node, increase a count by one. When you reach a node that points to None, it means the list ended. The count at this point is the length of the list. If the list is empty, the count stays zero. This method uses a simple loop checking if the current node exists, counting it, then moving to the next node until the end.