Bird
Raised Fist0
DSA Goprogramming~10 mins

Get Length of Linked List in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the length counter to zero.

DSA Go
func getLength(head *Node) int {
    length := [1]
    current := head
    for current != nil {
        length++
        current = current.next
    }
    return length
}
Drag options to blanks, or click blank then click option'
A1
B0
C-1
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Starting length at 1 causes incorrect counts for empty lists.
Using nil as initial length causes a type error.
2fill in blank
medium

Complete the code to move to the next node in the linked list.

DSA Go
func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        length++
        current = [1]
    }
    return length
}
Drag options to blanks, or click blank then click option'
Acurrent.next
Bhead.next
Clength.next
Dnext.current
Attempts:
3 left
💡 Hint
Common Mistakes
Using head.next inside the loop causes infinite loop or wrong traversal.
Using length.next is invalid because length is an int.
3fill in blank
hard

Fix the error in the loop condition to correctly check the end of the linked list.

DSA Go
func getLength(head *Node) int {
    length := 0
    current := head
    for [1] {
        length++
        current = current.next
    }
    return length
}
Drag options to blanks, or click blank then click option'
Acurrent == nil
Blength < 10
Chead != nil
Dcurrent != nil
Attempts:
3 left
💡 Hint
Common Mistakes
Using current == nil causes the loop to never run.
Using length < 10 limits counting to 10 nodes incorrectly.
4fill in blank
hard

Fill both blanks to create a map of node values to their positions in the list.

DSA Go
func mapValues(head *Node) map[int]int {
    result := make(map[int]int)
    current := head
    pos := 0
    for current != nil {
        result[[1]] = [2]
        current = current.next
        pos++
    }
    return result
}
Drag options to blanks, or click blank then click option'
Acurrent.value
Bpos
Ccurrent.pos
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.pos is invalid because nodes don't have a pos field.
Using value alone is undefined.
5fill in blank
hard

Fill all three blanks to create a slice of node values from the linked list.

DSA Go
func listValues(head *Node) []int {
    values := []int{}
    current := head
    for current != nil {
        values = append(values, [1])
        current = [2]
    }
    return [3]
}
Drag options to blanks, or click blank then click option'
Acurrent.value
Bcurrent.next
Cvalues
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Returning head instead of values returns the wrong data.
Using current instead of current.next causes infinite loop.

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