Bird
Raised Fist0
DSA Goprogramming~30 mins

Get Length of Linked List in DSA Go - Build from Scratch

Choose your learning style10 modes available
Get Length of Linked List
📖 Scenario: You are working on a simple contact list app. Each contact is stored as a node in a linked list. You want to find out how many contacts are currently saved.
🎯 Goal: Build a Go program that creates a linked list of contacts and calculates the total number of contacts by traversing the list.
📋 What You'll Learn
Create a linked list node struct called Node with an integer data field and a pointer to the next node called next.
Create a linked list with exactly three nodes containing the values 10, 20, and 30.
Create a variable called length to count the number of nodes in the list.
Use a for loop to traverse the linked list starting from the head node.
Print the length of the linked list.
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like contact lists, music playlists, and undo functionality where items are connected in order.
💼 Career
Understanding linked lists is fundamental for software developers, especially when working with dynamic data structures and memory management.
Progress0 / 4 steps
1
Create the linked list nodes
Create a struct called Node with an integer field data and a pointer to Node called next. Then create three nodes named node1, node2, and node3 with data values 10, 20, and 30 respectively. Link node1 to node2 and node2 to node3. Set node3.next to nil. Finally, create a variable head that points to node1.
DSA Go
Hint

Remember to define the struct first, then create nodes using &Node{data: value} syntax. Link nodes by setting the next pointer.

2
Create a length counter variable
Create an integer variable called length and set it to 0. This will count the number of nodes in the linked list.
DSA Go
Hint

Use length := 0 to create and initialize the counter variable.

3
Traverse the linked list to count nodes
Use a for loop with a variable current starting at head. Loop while current is not nil. Inside the loop, increase length by 1 and move current to the next node using current = current.next.
DSA Go
Hint

Use a for loop with current := head and update current = current.next each iteration. Increase length inside the loop.

4
Print the length of the linked list
Write a fmt.Println statement to print the text "Length of linked list:" followed by the value of length.
DSA Go
Hint

Use fmt.Println("Length of linked list:", length) to print the result.

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