-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedlist.go
159 lines (127 loc) · 2.85 KB
/
linkedlist.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gods
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
type LinkedList struct {
Head *ListNode
Length int
}
// Add a ListNode to the front of the list
func (l *LinkedList) Prepend(n *ListNode) {
second := l.Head
l.Head = n
l.Head.Next = second
l.Length++
}
// Add a ListNode to the end of the list
func (l *LinkedList) Append(n *ListNode) {
if l.Length == 0 {
l.Head = n
l.Length++
return
}
toAppend := l.Head
// Travel to the last node
for toAppend.Next != nil {
toAppend = toAppend.Next
}
toAppend.Next = n
l.Length++
}
// Add a ListNode with position
func (l *LinkedList) Insert(n *ListNode, position int) bool {
if position > l.Length {
return false
}
if position == 1 {
l.Prepend(n)
return true
}
if position == l.Length {
l.Append(n)
return true
}
toInsert := l.Head
// Move to the node next to the position. E.g. if position is 3, move to the second node.
for i := 1; i < position-1; i++ {
toInsert = toInsert.Next
}
// Point the new node to the previous node of the position (this node become the 4th node now)
n.Next = toInsert.Next
// Point the node next to the position to the new node ( 2 -> 3 )
toInsert.Next = n
l.Length++
return true
}
// toDelete means the previous node next to the node you want to delete
// Delete with value
func (l *LinkedList) DeleteWithValue(value int) {
// If the head is the value we want to delete
if l.Head.Val == value {
l.Head = l.Head.Next
l.Length--
return
}
toDelete := l.Head
// Travel to the node next to the node we want to delete
for toDelete.Next != nil {
// If the next node is the value we want to delete
if toDelete.Next.Val == value {
// Skip the node to delete
toDelete.Next = toDelete.Next.Next
l.Length--
return
}
// Move to the next node
toDelete = toDelete.Next
}
}
// Delete with position
func (l *LinkedList) Delete(position int) bool {
if position > l.Length {
return false
}
if position == 1 {
l.Head = l.Head.Next
l.Length--
return true
}
toDelete := l.Head
// Move to the node next to the position. E.g. if position is 3, move to the second node.
for i := 1; i < position-1; i++ {
toDelete = toDelete.Next
}
// Skip the node to delete
toDelete.Next = toDelete.Next.Next
l.Length--
return true
}
// Print the list
func (l LinkedList) PrintListVal() {
toPrint := l.Head
for l.Length != 0 {
fmt.Printf("%d ", toPrint.Val)
toPrint = toPrint.Next
l.Length--
}
fmt.Println("\n")
}
// Convert a list to a slice of int (for LeetCode)
func List2Ints(head *ListNode) []int {
res := []int{}
for head != nil {
res = append(res, head.Val)
head = head.Next
}
return res
}
// Convert a slice of int to a list (for LeetCode)
func Ints2List(nums []int) *ListNode {
var head *ListNode
for i := len(nums) - 1; i >= 0; i-- {
head = &ListNode{Val: nums[i], Next: head}
}
return head
}