Bird
0
0
DSA Cprogramming~10 mins

Get Length of Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Get Length of Linked List
Start at head node
Is current node NULL?
YesStop, length counted
No
Increment length count
Move to next node
Back to check current node
Start from the head node, check if current node exists, count it, move to next, repeat until no more nodes.
Execution Sample
DSA C
int getLength(Node* head) {
    int length = 0;
    Node* current = head;
    while (current != NULL) {
        length++;
        current = current->next;
    }
    return length;
}
Counts nodes in a linked list by traversing from head to end.
Execution Table
StepOperationCurrent NodeLength CountPointer ChangesVisual State
1Start at headNode10current = headNode1 -> Node2 -> Node3 -> NULL
2Check current != NULLNode10NoneNode1 -> Node2 -> Node3 -> NULL
3Increment lengthNode11NoneNode1 -> Node2 -> Node3 -> NULL
4Move to next nodeNode21current = current->nextNode1 -> Node2 -> Node3 -> NULL
5Check current != NULLNode21NoneNode1 -> Node2 -> Node3 -> NULL
6Increment lengthNode22NoneNode1 -> Node2 -> Node3 -> NULL
7Move to next nodeNode32current = current->nextNode1 -> Node2 -> Node3 -> NULL
8Check current != NULLNode32NoneNode1 -> Node2 -> Node3 -> NULL
9Increment lengthNode33NoneNode1 -> Node2 -> Node3 -> NULL
10Move to next nodeNULL3current = current->nextNode1 -> Node2 -> Node3 -> NULL
11Check current != NULLNULL3NoneNode1 -> Node2 -> Node3 -> NULL
12Stop traversalNULL3NoneFinal length = 3
💡 current becomes NULL at step 11, loop ends, length counted as 3
Variable Tracker
VariableStartAfter 1After 2After 3Final
length01233
currentNode1Node2Node3NULLNULL
Key Moments - 3 Insights
Why do we check current != NULL before counting length?
Because if current is NULL, it means we reached the end of the list and there are no more nodes to count. See execution_table step 11 where current is NULL and loop stops.
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 NULL)?
The loop never runs because current starts as NULL. Length remains 0. This is shown by the loop condition failing immediately at step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the length count at step 6?
A1
B2
C3
D0
💡 Hint
Check the 'Length Count' column at step 6 in the execution_table.
At which step does the current pointer become NULL?
AStep 9
BStep 11
CStep 10
DStep 12
💡 Hint
Look at the 'Current Node' column to see when it changes to NULL.
If the linked list had 5 nodes instead of 3, what would be the final length?
A5
B4
C3
D6
💡 Hint
Length equals the number of nodes counted, so final length matches node count.
Concept Snapshot
Get Length of Linked List:
- Start at head node
- While current node is not NULL:
  - Increment length
  - Move to next node
- Stop when current is NULL
- Return length counted
Full Transcript
To get the length of a linked list, start from the head node. Check if the current node exists. If yes, increase the length count by one. Then move to the next node. Repeat this until you reach a node that is NULL, which means the end of the list. The length count at this point is the total number of nodes in the list.