-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseLinkedList.go
58 lines (51 loc) · 919 Bytes
/
reverseLinkedList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build ignore
package main
import "fmt"
type Node struct {
Value int
Next *Node
}
func build(arr []int) *Node {
var head *Node
for i := len(arr) - 1; i >= 0; i-- {
head = &Node{Value: arr[i], Next: head}
}
return head
}
func traverse(head *Node) {
if head == nil {
fmt.Println("The list is empty.")
return
}
current := head
for current != nil {
if current.Next != nil {
fmt.Print(current.Value, "->")
} else {
fmt.Print(current.Value)
}
current = current.Next
}
fmt.Println()
}
func reverseList(head *Node) *Node {
var prev *Node
curr := head
for curr != nil {
tmp := curr.Next
curr.Next = prev
prev = curr
curr = tmp
}
return prev
}
func main() {
arr1 := []int{0, 1, 2, 3}
head1 := build(arr1)
reversedHead1 := reverseList(head1)
traverse(reversedHead1)
arr2 := []int{}
head2 := build(arr2)
reversedHead2 := reverseList(head2)
traverse(reversedHead2)
}