📖 Scenario: Imagine you want to keep track of your daily tasks in order. A singly linked list is like a chain where each task points to the next one. You will build this chain step-by-step in Go.
🎯 Goal: Build a simple singly linked list in Go that holds three tasks and prints them in order.
📋 What You'll Learn
Define a Node struct with data and next fields
Create three nodes with exact task names: "Task1", "Task2", "Task3"
Link the nodes to form a chain
Print the tasks in order separated by -> and ending with nil
💡 Why This Matters
🌍 Real World
Singly linked lists are used in many applications like task scheduling, undo functionality, and managing playlists where order matters.
💼 Career
Understanding linked lists is fundamental for software developers, especially for roles involving system design, memory management, and algorithm optimization.
Progress0 / 4 steps
1
Define the Node struct and create the first node
Define a struct called Node with two fields: data of type string and next of type *Node. Then create a variable called head of type *Node and assign it a new Node with data set to "Task1" and next set to nil.
DSA Go
Hint
Remember, next points to the next node or nil if none.
2
Create the second and third nodes and link them
Create two more variables called second and third of type *Node. Assign second a new Node with data set to "Task2" and next set to nil. Assign third a new Node with data set to "Task3" and next set to nil. Then link the nodes by setting head.next to second and second.next to third.
DSA Go
Hint
Each node's next points to the following node.
3
Traverse the linked list and collect task names
Create a variable called current and set it to head. Create a slice of strings called tasks to store the task names. Use a for loop that continues while current is not nil. Inside the loop, append current.data to tasks and then set current to current.next.
DSA Go
Hint
Use for current != nil to loop through the nodes.
4
Print the linked list tasks in order
Use a for loop with index i to iterate over the tasks slice. For each task except the last, print the task followed by " -> " without a newline. For the last task, print the task followed by " -> nil" with a newline.
DSA Go
Hint
Use fmt.Print for tasks except the last, and fmt.Println for the last task.
Practice
(1/5)
1. What is the main purpose of a singly linked list in data structures?
easy
A. To store elements in a hash table for fast lookup
B. To store elements in a tree-like structure with multiple branches
C. To store elements in a fixed-size array for quick access
D. To store elements in a linear order where each element points to the next
Solution
Step 1: Understand singly linked list structure
A singly linked list stores elements in nodes where each node points to the next node, forming a chain.
Step 2: Compare with other data structures
Unlike trees or arrays, singly linked lists have a linear sequence with pointers only to the next element.
Final Answer:
To store elements in a linear order where each element points to the next -> Option D
Quick Check:
Singly linked list = linear chain with next pointers [OK]
Hint: Remember: singly linked list nodes point only forward [OK]
Common Mistakes:
Confusing singly linked list with trees or arrays
Thinking nodes have pointers to previous nodes
Assuming fixed size storage like arrays
2. Which of the following is the correct way to define a node struct for a singly linked list in Go?
easy
A. type Node struct { value int; next *Node }
B. type Node struct { value int; prev *Node }
C. type Node struct { value int; next Node }
D. type Node struct { value int; next interface{} }
Solution
Step 1: Identify correct pointer usage
In Go, the next node must be a pointer to Node, so *Node is correct.
Step 2: Check struct fields
Field 'value' stores data, 'next' points to the next node. 'prev' is for doubly linked lists, and 'interface{}' is too generic.
Final Answer:
type Node struct { value int; next *Node } -> Option A
Quick Check:
Node next pointer = *Node [OK]
Hint: Use *Node for next pointer in Go struct [OK]
Common Mistakes:
Using value type instead of pointer for next
Confusing singly linked list with doubly linked list (prev pointer)
Using interface{} instead of *Node for next
3. What will be the output of the following Go code that creates a singly linked list with nodes 1 -> 2 -> 3?
package main
import "fmt"
type Node struct {
value int
next *Node
}
func main() {
head := &Node{value: 1}
second := &Node{value: 2}
third := &Node{value: 3}
head.next = second
second.next = third
current := head
for current != nil {
fmt.Print(current.value)
if current.next != nil {
fmt.Print(" -> ")
}
current = current.next
}
fmt.Println(" -> null")
}
medium
A. 1 -> 2 -> null
B. 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> null
D. Compilation error
Solution
Step 1: Trace node creation and linking
Nodes with values 1, 2, 3 are created and linked in order: head.next = second, second.next = third.
Step 2: Trace printing loop
Loop prints current.value, then ' -> ' if next exists, moving from head to third, then prints ' -> null'.
Final Answer:
1 -> 2 -> 3 -> null -> Option C
Quick Check:
Print nodes in order 1,2,3 with arrows [OK]
Hint: Follow next pointers from head to end [OK]
Common Mistakes:
Printing nodes in reverse order
Missing last node in output
Confusing pointer assignments causing nil errors
4. Identify the error in this Go code snippet that tries to append a new node with value 4 to the end of a singly linked list:
func appendNode(head *Node, val int) {
newNode := &Node{value: val}
current := head
for current.next != nil {
current = current.next
}
current = newNode
}
medium
A. The loop condition should be current != nil instead of current.next != nil
B. The new node is not linked to the list; current pointer is reassigned incorrectly
C. The new node should be created with next pointing to head
D. The function should return the new head pointer
Solution
Step 1: Analyze the loop and pointer update
The loop correctly finds the last node where current.next is nil.
Step 2: Check how new node is linked
Assigning current = newNode only changes the local variable, not the last node's next pointer. It should be current.next = newNode.
Final Answer:
The new node is not linked to the list; current pointer is reassigned incorrectly -> Option B
Quick Check:
Link new node by current.next = newNode [OK]
Hint: Assign new node to current.next, not current [OK]
5. You want to create a function in Go that reverses a singly linked list. Which approach correctly reverses the list by changing the next pointers?
func reverseList(head *Node) *Node {
var prev *Node = nil
current := head
for current != nil {
nextTemp := current.next
current.next = prev
prev = current
current = nextTemp
}
return prev
}
What will be the output if the original list is 1 -> 2 -> 3 -> null and you print the reversed list?
hard
A. 3 -> 2 -> 1 -> null
B. 1 -> 2 -> 3 -> null
C. null
D. Runtime error due to nil pointer
Solution
Step 1: Understand reversal logic
The function iterates through the list, reversing each node's next pointer to point to the previous node.
Step 2: Trace reversal on list 1 -> 2 -> 3
After reversal, the list becomes 3 -> 2 -> 1 -> null, and prev points to new head.
Final Answer:
3 -> 2 -> 1 -> null -> Option A
Quick Check:
Reversed list head = prev = 3 [OK]
Hint: Reverse pointers by tracking prev, current, next [OK]
Common Mistakes:
Not updating current.next correctly
Losing track of next node causing infinite loop
Returning old head instead of new head
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.