Mental Model
Go through each item in the list one by one from start to end and show its value.
Analogy: Like reading a line of people holding hands, looking at each person's face in order until the last person.
head -> 1 -> 2 -> 3 -> null
head -> 1 -> 2 -> 3 -> null
[curr->1] -> 2 -> 3 -> null
1 -> [curr->2] -> 3 -> null
1 -> 2 -> [curr->3] -> null
1 -> 2 -> 3 -> [curr->null]
1 -> 2 -> 3 -> null
package main import "fmt" type Node struct { value int next *Node } func printList(head *Node) { curr := head for curr != nil { fmt.Print(curr.value) if curr.next != nil { fmt.Print(" -> ") } curr = curr.next } fmt.Print(" -> null\n") } func main() { // Create linked list 1 -> 2 -> 3 -> null node3 := &Node{value: 3, next: nil} node2 := &Node{value: 2, next: node3} node1 := &Node{value: 1, next: node2} printList(node1) }
curr := headfor curr != nil {fmt.Print(curr.value)if curr.next != nil { fmt.Print(" -> ") }curr = curr.nextfor curr != nil {if curr.next != nil { fmt.Print(" -> ") }
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")
}type Node struct {
value int
next *Node
}
func printList(head *Node) {
node := head
for node != nil {
fmt.Println(node.value)
node = node.prev
}
}func printEven(head *Node) {
for node := head; node != nil; node = node.next {
// Your code here
}
}Pass the test and this pattern moves unknown