📖 Scenario: You are working on a simple contact list app. Each contact is stored as a node in a linked list. You want to see all the contacts by traversing the list and printing each contact's name.
🎯 Goal: Build a linked list of contacts, then write code to traverse and print all contact names in order.
📋 What You'll Learn
Create a linked list node struct called ContactNode with fields name (string) and next (*ContactNode).
Create three linked list nodes with names exactly "Alice", "Bob", and "Charlie" linked in that order.
Create a variable called head that points to the first node (Alice).
Write a for loop to traverse the linked list starting from head.
Linked lists are used in many apps to store ordered data where size can change, like contact lists, playlists, or undo history.
💼 Career
Understanding linked lists helps in software development roles that require managing dynamic data efficiently, such as backend development and systems programming.
Progress0 / 4 steps
1
Create the linked list nodes
Create a struct called ContactNode with fields name string and next *ContactNode. Then create three variables node1, node2, and node3 of type ContactNode with names "Alice", "Bob", and "Charlie" respectively. Link node1.next to &node2 and node2.next to &node3. Set node3.next to nil.
DSA Go
Hint
Define the struct first. Then create nodes from last to first or link them after creation. Use next *ContactNode for the pointer to the next node.
2
Create the head pointer
Create a variable called head and set it to point to &node1, the first node in the linked list.
DSA Go
Hint
Use head := &node1 to point to the first node.
3
Traverse the linked list
Write a for loop that uses a variable current starting at head. Loop while current != nil. Inside the loop, if current.next != nil print current.name + " -> ", else print current.name + " -> null". Then move current to current.next.
DSA Go
Hint
Use a for loop with current != nil. Print current.name + " -> " if next exists, else print current.name + " -> null". Move current forward each time.
4
Print the linked list
Add a fmt.Println() after the loop to move to the next line after printing the linked list.
DSA Go
Hint
Use fmt.Println() to print a newline after the loop.
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
Step 1: Understand traversal meaning
Traversal means going through each node in order, starting from the head.
Step 2: Purpose of traversal
Visiting each node allows us to read or process data stored in the list.
Final Answer:
To visit each node in the list one by one -> Option C
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
Step 1: Identify correct loop start and condition
Traversal starts at head and continues while node is not nil.
Step 2: Correct pointer update
Move to next node by node = node.next each iteration.
Final Answer:
for node := head; node != nil; node = node.next {} -> Option A
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
Step 1: Understand list structure
n1 points to n2, n2 points to n3, n3 points to nil. Values are 1, 2, 3 in order.
Step 2: Trace the loop output
Loop prints node.value followed by ' -> ' for each node: 1 -> 2 -> 3 -> then prints 'nil'.
Final Answer:
1 -> 2 -> 3 -> nil -> Option D
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
Step 1: Check struct fields
Node struct has 'next' pointer but no 'prev' pointer.
Step 2: Identify incorrect pointer usage
Code uses 'node = node.prev' which is invalid and causes compile error.
Final Answer:
Using 'node.prev' which doesn't exist in singly linked list -> Option A
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
Step 1: Use conditional inside traversal
Check if node.value is even using modulo operator inside the loop.
Step 2: Print only even values
Print node.value with arrow only if condition is true.
Final Answer:
Add 'if node.value%2 == 0 { fmt.Print(node.value, " -> ") }' inside the loop -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.