Bird
Raised Fist0
DSA Goprogramming

Get Length of Linked List in DSA Go

Choose your learning style10 modes available
Mental Model
Count how many nodes are in the linked list by moving from the start to the end one by one.
Analogy: Like counting people in a line by asking each person to point to the next until no one is left.
head -> [1] -> [2] -> [3] -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Find how many nodes are in the list
Step 1: start at head node with value 1
head -> [1 ↑] -> [2] -> [3] -> null
Why: We begin counting from the first node
Step 2: move to next node with value 2, count = 2
head -> [1] -> [2 ↑] -> [3] -> null
Why: We count the second node by moving forward
Step 3: move to next node with value 3, count = 3
head -> [1] -> [2] -> [3 ↑] -> null
Why: We count the third node by moving forward
Step 4: next is null, stop counting
head -> [1] -> [2] -> [3] -> null
Why: No more nodes to count, end here
Result:
head -> [1] -> [2] -> [3] -> null
Length = 3
Annotated Code
DSA Go
package main

import "fmt"

type Node struct {
	value int
	next  *Node
}

func getLength(head *Node) int {
	count := 0
	current := head
	for current != nil {
		count++ // increment count for current node
		current = current.next // move to next node
	}
	return count
}

func main() {
	// create linked list 1 -> 2 -> 3 -> null
	node3 := &Node{value: 3, next: nil}
	node2 := &Node{value: 2, next: node3}
	head := &Node{value: 1, next: node2}

	length := getLength(head)
	fmt.Printf("Length = %d\n", length)
}
for current != nil {
loop through each node until end of list
count++ // increment count for current node
count each node visited
current = current.next // move to next node
advance to next node to continue counting
OutputSuccess
Length = 3
Complexity Analysis
Time: O(n) because we visit each of the n nodes once to count them
Space: O(1) because we only use a few variables regardless of list size
vs Alternative: This is optimal; no alternative can count nodes without visiting them all
Edge Cases
empty list (head is nil)
returns length 0 because there are no nodes
DSA Go
for current != nil {
single node list
returns length 1 after counting the single node
DSA Go
count++ // increment count for current node
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 you reach the end.
Common Mistakes
Mistake: Forgetting to move to the next node inside the loop, causing an infinite loop
Fix: Add 'current = current.next' inside the loop to advance the pointer
Mistake: Starting count at 1 instead of 0, which gives wrong length for empty lists
Fix: Initialize count to 0 before counting nodes
Summary
Counts the number of nodes in a linked list by traversing it.
Use when you need to know the size of a linked list.
The key is to move through each node one by one and count them.

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