Bird
Raised Fist0
DSA Goprogramming~20 mins

Traversal and Printing a Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Traversing a Linked List
What is the printed output of the following Go code that traverses and prints a singly linked list?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func main() {
    head := &Node{value: 1}
    head.next = &Node{value: 2}
    head.next.next = &Node{value: 3}
    current := head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Print("null\n")
}
A1 -> 2 -> null
Bnull
C3 -> 2 -> 1 -> null
D1 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Trace the loop from head to the end, printing each node's value.
❓ Predict Output
intermediate
2:00remaining
Detecting Output After Modifying Linked List
What is the output after running this Go code that modifies a linked list before printing?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func main() {
    head := &Node{value: 10}
    head.next = &Node{value: 20}
    head.next.next = &Node{value: 30}
    head.next = head.next.next
    current := head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Print("null\n")
}
A10 -> 20 -> 30 -> null
B10 -> 30 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Look at how the next pointer of the head node is changed before printing.
🔧 Debug
advanced
2:00remaining
Identify the Error in Linked List Traversal
What error does this Go code produce when trying to traverse and print a linked list?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func main() {
    head := &Node{value: 5}
    head.next = &Node{value: 10}
    current := head
    for current.next != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Print("null\n")
}
AThe last node's value (10) is not printed
BRuntime panic due to nil pointer dereference
CSyntax error: missing colon in for loop
DInfinite loop printing 5 repeatedly
Attempts:
2 left
💡 Hint
Check the loop condition and what nodes get printed inside the loop.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Linked List Print
What is the output of this Go code that prints a linked list recursively?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func printList(node *Node) {
    if node == nil {
        fmt.Print("null\n")
        return
    }
    fmt.Printf("%d -> ", node.value)
    printList(node.next)
}
func main() {
    head := &Node{value: 7}
    head.next = &Node{value: 14}
    head.next.next = &Node{value: 21}
    printList(head)
}
A7 -> 14 -> 21 -> null
Bnull
C7 -> 14 -> null
D21 -> 14 -> 7 -> null
Attempts:
2 left
💡 Hint
Follow the recursive calls printing each node's value before the next call.
🧠 Conceptual
expert
2:00remaining
Number of Nodes After Multiple Insertions
Starting with an empty linked list, the following insertions happen in order: insert 1 at head, insert 2 at head, insert 3 at tail, insert 4 at tail. How many nodes does the list contain after these operations?
A3
B5
C4
D2
Attempts:
2 left
💡 Hint
Count each insertion carefully, noting head and tail insertions add nodes.

Practice

(1/5)
1. What is the main purpose of traversing a linked list?
easy
A. To create a new linked list from scratch
B. To delete all nodes at once
C. To visit each node in the list one by one
D. To reverse the list without visiting nodes

Solution

  1. Step 1: Understand traversal meaning

    Traversal means going through each node in order, starting from the head.
  2. Step 2: Purpose of traversal

    Visiting each node allows us to read or process data stored in the list.
  3. Final Answer:

    To visit each node in the list one by one -> Option C
  4. Quick Check:

    Traversal = visiting nodes [OK]
Hint: Traversal means visiting nodes one by one [OK]
Common Mistakes:
  • Confusing traversal with deletion
  • Thinking traversal creates a new list
  • Assuming traversal reverses the list
2. Which of the following is the correct way to start traversing a singly linked list in Go?
easy
A. for node := head; node != nil; node = node.next {}
B. for node := head; node == nil; node = node.next {}
C. for node := nil; node != head; node = node.next {}
D. for node := head.next; node != nil; node = node.prev {}

Solution

  1. Step 1: Identify correct loop start and condition

    Traversal starts at head and continues while node is not nil.
  2. Step 2: Correct pointer update

    Move to next node by node = node.next each iteration.
  3. Final Answer:

    for node := head; node != nil; node = node.next {} -> Option A
  4. Quick Check:

    Start at head, loop until nil, move next [OK]
Hint: Start at head, loop until nil, move to next [OK]
Common Mistakes:
  • Using node == nil as loop condition (wrong)
  • Starting from nil instead of head
  • Using node.prev in singly linked list
3. What will be the output of the following Go code snippet that traverses and prints a linked list?
type Node struct {
  value int
  next  *Node
}

func main() {
  n3 := &Node{3, nil}
  n2 := &Node{2, n3}
  n1 := &Node{1, n2}
  for node := n1; node != nil; node = node.next {
    fmt.Print(node.value, " -> ")
  }
  fmt.Print("nil")
}
medium
A. 3 -> 2 -> 1 -> nil
B. 1 2 3 nil
C. nil -> 1 -> 2 -> 3
D. 1 -> 2 -> 3 -> nil

Solution

  1. Step 1: Understand list structure

    n1 points to n2, n2 points to n3, n3 points to nil. Values are 1, 2, 3 in order.
  2. Step 2: Trace the loop output

    Loop prints node.value followed by ' -> ' for each node: 1 -> 2 -> 3 -> then prints 'nil'.
  3. Final Answer:

    1 -> 2 -> 3 -> nil -> Option D
  4. Quick Check:

    Print order matches list order [OK]
Hint: Print nodes in order, end with nil [OK]
Common Mistakes:
  • Printing values in reverse order
  • Missing '->' arrows
  • Printing nil at start instead of end
4. Identify the error in this Go code snippet that tries to print a linked list:
type Node struct {
  value int
  next  *Node
}

func printList(head *Node) {
  node := head
  for node != nil {
    fmt.Println(node.value)
    node = node.prev
  }
}
medium
A. Using 'node.prev' which doesn't exist in singly linked list
B. Missing initialization of 'node' variable
C. Incorrect loop condition 'node != nil' causes infinite loop
D. fmt.Println cannot print integers directly

Solution

  1. Step 1: Check struct fields

    Node struct has 'next' pointer but no 'prev' pointer.
  2. Step 2: Identify incorrect pointer usage

    Code uses 'node = node.prev' which is invalid and causes compile error.
  3. Final Answer:

    Using 'node.prev' which doesn't exist in singly linked list -> Option A
  4. Quick Check:

    prev pointer missing in singly linked list [OK]
Hint: Singly linked list has only 'next', not 'prev' [OK]
Common Mistakes:
  • Using prev pointer in singly linked list
  • Assuming fmt.Println can't print ints
  • Thinking loop condition is wrong
5. Given a singly linked list, how would you modify the traversal code to print only the nodes with even values?
func printEven(head *Node) {
  for node := head; node != nil; node = node.next {
    // Your code here
  }
}
hard
A. Use 'if node.value == 0 { fmt.Print(node.value) }' inside the loop
B. Add 'if node.value%2 == 0 { fmt.Print(node.value, " -> ") }' inside the loop
C. Change loop to 'for node := head; node.value%2 == 0; node = node.next {}'
D. Print all nodes and then filter even values after traversal

Solution

  1. Step 1: Use conditional inside traversal

    Check if node.value is even using modulo operator inside the loop.
  2. Step 2: Print only even values

    Print node.value with arrow only if condition is true.
  3. Final Answer:

    Add 'if node.value%2 == 0 { fmt.Print(node.value, " -> ") }' inside the loop -> Option B
  4. Quick Check:

    Condition inside loop filters even nodes [OK]
Hint: Use if condition inside loop to filter even values [OK]
Common Mistakes:
  • Changing loop condition incorrectly
  • Checking equality to zero instead of evenness
  • Printing all nodes then filtering later
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