Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Get Length of Linked List in DSA Python - Execution Trace

Choose your learning style10 modes available
Concept Flow - Get Length of Linked List
Start at head node
↓
Is current node None?
Yes→Stop, 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.

Practice

(1/5)
1. What does the length of a linked list represent?
easy
A. The memory size of the linked list
B. The value of the first node
C. The number of nodes in the linked list
D. The value of the last node

Solution

  1. Step 1: Understand linked list structure

    A linked list is made of nodes connected one after another.
  2. Step 2: Define length meaning

    The length is how many nodes are present in the list.
  3. Final Answer:

    The number of nodes in the linked list -> Option C
  4. Quick Check:

    Length counts nodes = number of nodes [OK]
Hint: Length = count of nodes from head to end [OK]
Common Mistakes:
  • Confusing length with node values
  • Thinking length is memory size
  • Counting only first or last node
2. Which of the following Python code snippets correctly initializes a variable to count the length of a linked list?
easy
A. length = None
B. length = 0
C. length = head
D. length = []

Solution

  1. Step 1: Identify counting variable type

    Length should be a number starting at zero before counting nodes.
  2. Step 2: Check each option

    length = 0 sets length to 0, ready to increment. Others assign wrong types.
  3. Final Answer:

    length = 0 -> Option B
  4. Quick Check:

    Initialize count as 0 before loop [OK]
Hint: Start counting length from zero [OK]
Common Mistakes:
  • Initializing length as None or list
  • Assigning length to head node
  • Using wrong data types for count
3. What will be the output of the following code?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def get_length(head):
    count = 0
    current = head
    while current:
        count += 1
        current = current.next
    return count

# Creating linked list: 10 -> 20 -> 30 -> None
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

print(get_length(head))
medium
A. 3
B. 2
C. 0
D. None

Solution

  1. Step 1: Trace linked list nodes

    List has nodes with values 10, 20, and 30, total 3 nodes.
  2. Step 2: Follow get_length function

    Function counts nodes by moving current pointer until None, increments count each step.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Count nodes 10,20,30 = 3 [OK]
Hint: Count nodes by traversing until None [OK]
Common Mistakes:
  • Stopping count early
  • Not incrementing count inside loop
  • Returning None instead of count
4. Find the error in this code to get the length of a linked list:
def get_length(head):
    count = 0
    while head.next:
        count += 1
        head = head.next
    return count
medium
A. It misses counting the last node
B. It causes infinite loop
C. It returns None instead of count
D. It counts nodes twice

Solution

  1. Step 1: Analyze loop condition

    Loop runs while head.next exists, so it stops before last node.
  2. Step 2: Check counting logic

    Last node is not counted because loop ends early, count misses one node.
  3. Final Answer:

    It misses counting the last node -> Option A
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Use 'while current' not 'while current.next' to count all nodes [OK]
Common Mistakes:
  • Using head.next in loop condition
  • Not counting last node
  • Assuming loop covers all nodes
5. You have a linked list where each node contains a number. Write a function to find the length of the linked list only if all node values are positive. If any node has zero or negative value, return 0 immediately. What is the correct approach?
hard
A. Check only first node value, then count all nodes
B. Count nodes first, then check values in a second pass
C. Return length without checking values
D. Traverse nodes, if value <= 0 return 0, else count nodes till end

Solution

  1. Step 1: Traverse and check values simultaneously

    Move through each node, check if value is positive.
  2. Step 2: Return 0 immediately if value is zero or negative, else keep counting

    If any node fails condition, stop and return 0; otherwise, return total count.
  3. Final Answer:

    Traverse nodes, if value <= 0 return 0, else count nodes till end -> Option D
  4. Quick Check:

    Check values while counting, stop early if invalid [OK]
Hint: Check values during traversal, stop early if invalid [OK]
Common Mistakes:
  • Counting first, then checking values
  • Ignoring node values
  • Checking only first node value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard