Bird
Raised Fist0
DSA Goprogramming~10 mins

Traversal and Printing a 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 start traversing the linked list from the head node.

DSA Go
current := [1]
Drag options to blanks, or click blank then click option'
Ahead
Btail
Cnil
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Starting traversal from tail instead of head
Assigning nil instead of head
Using next without a current node
2fill in blank
medium

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

DSA Go
current = current.[1]
Drag options to blanks, or click blank then click option'
Anext
Bhead
Cprev
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists
Using 'head' which is the start, not the next node
Using 'value' which holds data, not the link
3fill in blank
hard

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

DSA Go
for current != [1] {
    fmt.Println(current.value)
    current = current.next
}
Drag options to blanks, or click blank then click option'
Atail
Bnil
Chead
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using head in the condition which causes infinite loop
Using tail which is not a pointer in singly linked list
Using current which never changes the condition
4fill in blank
hard

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

DSA Go
positions := make(map[int]int)
index := 0
for current != nil {
    positions[[1]] = index
    current = current.[2]
    index++
}
Drag options to blanks, or click blank then click option'
Acurrent.value
Bnext
Cindex
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using index as key instead of node value
Not moving current to next node
Using current instead of current.value as map key
5fill in blank
hard

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

DSA Go
values := []int{}
for current != nil {
    values = append(values, [1])
    current = current.[2]
}
fmt.Println(values[[3]])
Drag options to blanks, or click blank then click option'
Acurrent.value
Bnext
C0
Dcurrent.next
Attempts:
3 left
💡 Hint
Common Mistakes
Appending current instead of current.value
Using 'current.next' instead of 'next' to move forward
Printing values at wrong index

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