Bird
Raised Fist0
DSA Goprogramming~10 mins

Get Length of Linked List in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Get Length of Linked List
Start at head node
↓
Is current node nil?
Yes→Return count
No↓
Increment count
↓
Move to next node
↩Back to check current node
Start from the head node, count each node until reaching the end (nil), then return the count.
Execution Sample
DSA Go
func getLength(head *Node) int {
    count := 0
    current := head
    for current != nil {
        count++
        current = current.next
    }
    return count
}
Counts the number of nodes in a linked list by traversing from head to end.
Execution Table
StepOperationCurrent NodeCountPointer ChangesVisual State
1Initialize count=0, current=headNode(1)0current=head1 -> 2 -> 3 -> nil
2Check current != nilNode(1)0No change1 -> 2 -> 3 -> nil
3Increment countNode(1)1No change1 -> 2 -> 3 -> nil
4Move current to nextNode(2)1current = current.next1 -> 2 -> 3 -> nil
5Check current != nilNode(2)1No change1 -> 2 -> 3 -> nil
6Increment countNode(2)2No change1 -> 2 -> 3 -> nil
7Move current to nextNode(3)2current = current.next1 -> 2 -> 3 -> nil
8Check current != nilNode(3)2No change1 -> 2 -> 3 -> nil
9Increment countNode(3)3No change1 -> 2 -> 3 -> nil
10Move current to nextnil3current = current.next1 -> 2 -> 3 -> nil
11Check current != nilnil3No change1 -> 2 -> 3 -> nil
12Return countnil3No change1 -> 2 -> 3 -> nil
💡 current becomes nil at step 11, loop ends and count 3 is returned
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 10Final
count01233
currentNode(1)Node(2)Node(3)nilnil
Key Moments - 3 Insights
Why do we check current != nil instead of current.next != nil?
Because we want to count the current node itself before moving to next. Checking current != nil ensures we count all nodes including the last one (see steps 8-11 in execution_table).
What happens if the linked list is empty (head is nil)?
The loop condition current != nil fails immediately (step 2), so count remains 0 and is returned (step 12).
Why do we move current to current.next after incrementing count?
We count the current node first, then move to the next node to continue counting. This order ensures no node is skipped (see steps 3-4, 6-7, 9-10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 6?
A2
B1
C3
D0
💡 Hint
Check the 'Count' column at step 6 in the execution_table.
At which step does current become nil?
AStep 11
BStep 10
CStep 9
DStep 12
💡 Hint
Look at the 'Current Node' column to see when it changes to nil.
If the linked list had 5 nodes instead of 3, how many times would count increment?
A3
B4
C5
D6
💡 Hint
Count increments once per node, so check the 'count' variable changes in variable_tracker.
Concept Snapshot
Get Length of Linked List:
- Start at head node
- Initialize count = 0
- While current node is not nil:
  - Increment count
  - Move to next node
- Return count
Counts all nodes including last one.
Full Transcript
To get the length of a linked list, start from the head node and set count to zero. Then, check if the current node is not nil. If it is not nil, increment count by one and move to the next node. Repeat this until the current node becomes nil, which means the end of the list is reached. Finally, return the count which represents the number of nodes in the list. This method counts every node including the last one. If the list is empty, count remains zero and is returned immediately.

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 sum of all node values

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 counts 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 = count of nodes [OK]
Hint: Length means count nodes, not values or memory [OK]
Common Mistakes:
  • Confusing length with sum of values
  • Thinking length is memory size
  • Using first node value as length
2. Which Go code snippet correctly initializes a variable to count the length of a linked list?
easy
A. length := 0
B. length := nil
C. length := "0"
D. length := false

Solution

  1. Step 1: Identify correct variable type for counting

    Length is a number, so it should be an integer starting at zero.
  2. Step 2: Check Go syntax for initialization

    Using length := 0 correctly declares an int variable with zero value.
  3. Final Answer:

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

    Count variable starts at 0 [OK]
Hint: Count starts at zero as int, not nil or string [OK]
Common Mistakes:
  • Using nil instead of zero
  • Using string "0" instead of int 0
  • Using boolean false instead of number
3. What is the output of this Go code that counts the length of a linked list?
type Node struct {
    value int
    next  *Node
}

func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        length++
        current = current.next
    }
    return length
}

// Linked list: 10 -> 20 -> 30 -> nil
head := &Node{10, &Node{20, &Node{30, nil}}}
fmt.Println(getLength(head))
medium
A. 2
B. 3
C. 0
D. Runtime error

Solution

  1. Step 1: Trace the loop through nodes

    Start at head (10), count 1, move to next (20), count 2, move to next (30), count 3, next is nil, stop.
  2. Step 2: Return the count

    The loop counted 3 nodes, so length is 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Nodes counted = 3 [OK]
Hint: Count increments once per node until nil [OK]
Common Mistakes:
  • Stopping loop too early
  • Not incrementing length inside loop
  • Confusing nil check
4. Find the error in this Go function that tries to get the length of a linked list:
func getLength(head *Node) int {
    length := 0
    for head.next != nil {
        length++
        head = head.next
    }
    return length
}
medium
A. It misses counting the last node
B. It causes a nil pointer dereference
C. It counts nodes twice
D. It returns length as a string

Solution

  1. Step 1: Analyze loop condition

    The loop runs while head.next != nil, so it stops before the last node.
  2. Step 2: Check counting logic

    Since last node's next is nil, it is not counted, so length is one less than actual.
  3. Final Answer:

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

    Loop condition excludes last node [OK]
Hint: Loop until current != nil to count all nodes [OK]
Common Mistakes:
  • Using head.next != nil instead of head != nil
  • Not counting last node
  • Assuming loop covers all nodes
5. Given a linked list where each node contains an integer, how would you modify the length function to count only nodes with even values?
func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        // Your condition here
        current = current.next
    }
    return length
}
hard
A. Return length without any loop
B. Increment length unconditionally inside the loop
C. Check if current.value > 0 before incrementing length
D. Add if current.value%2 == 0 { length++ } inside the loop

Solution

  1. Step 1: Add condition to count only even values

    Inside the loop, check if current.value is divisible by 2 (even).
  2. Step 2: Increment length only if condition is true

    Use if current.value%2 == 0 { length++ } to count even nodes.
  3. Final Answer:

    Add if current.value%2 == 0 { length++ } inside the loop -> Option D
  4. Quick Check:

    Count only even nodes [OK]
Hint: Use modulo 2 check inside loop to count even nodes [OK]
Common Mistakes:
  • Counting all nodes without condition
  • Checking wrong condition like > 0
  • Skipping loop entirely
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