-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
singlylinkedlist.go
181 lines (152 loc) · 3.53 KB
/
singlylinkedlist.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package linkedlist
// demonstration of singly linked list in golang
import (
"errors"
"fmt"
)
// Singly structure with length of the list and its head
type Singly[T any] struct {
length int
// Note that Node here holds both Next and Prev Node
// however only the Next node is used in Singly methods.
Head *Node[T]
}
// NewSingly returns a new instance of a linked list
func NewSingly[T any]() *Singly[T] {
return &Singly[T]{}
}
// AddAtBeg adds a new snode with given value at the beginning of the list.
func (ll *Singly[T]) AddAtBeg(val T) {
n := NewNode(val)
n.Next = ll.Head
ll.Head = n
ll.length++
}
// AddAtEnd adds a new snode with given value at the end of the list.
func (ll *Singly[T]) AddAtEnd(val T) {
n := NewNode(val)
if ll.Head == nil {
ll.Head = n
ll.length++
return
}
cur := ll.Head
for ; cur.Next != nil; cur = cur.Next {
}
cur.Next = n
ll.length++
}
// DelAtBeg deletes the snode at the head(beginning) of the list
// and returns its value. Returns false if the list is empty.
func (ll *Singly[T]) DelAtBeg() (T, bool) {
if ll.Head == nil {
var r T
return r, false
}
cur := ll.Head
ll.Head = cur.Next
ll.length--
return cur.Val, true
}
// DelAtEnd deletes the snode at the tail(end) of the list
// and returns its value. Returns false if the list is empty.
func (ll *Singly[T]) DelAtEnd() (T, bool) {
if ll.Head == nil {
var r T
return r, false
}
if ll.Head.Next == nil {
return ll.DelAtBeg()
}
cur := ll.Head
for ; cur.Next.Next != nil; cur = cur.Next {
}
retval := cur.Next.Val
cur.Next = nil
ll.length--
return retval, true
}
// DelByPos deletes the node at the middle based on position in the list
// and returns its value. Returns false if the list is empty or length is not more than given position
func (ll *Singly[T]) DelByPos(pos int) (T, bool) {
switch {
case ll.Head == nil:
var r T
return r, false
case pos-1 > ll.length:
var r T
return r, false
case pos-1 == 0:
return ll.DelAtBeg()
case pos-1 == ll.Count():
return ll.DelAtEnd()
}
var prev *Node[T]
var val T
cur := ll.Head
count := 0
for count < pos-1 {
prev = cur
cur = cur.Next
count++
}
val = cur.Val
prev.Next = cur.Next
ll.length--
return val, true
}
// Count returns the current size of the list.
func (ll *Singly[T]) Count() int {
return ll.length
}
// Reverse reverses the list.
func (ll *Singly[T]) Reverse() {
var prev, Next *Node[T]
cur := ll.Head
for cur != nil {
Next = cur.Next
cur.Next = prev
prev = cur
cur = Next
}
ll.Head = prev
}
// ReversePartition Reverse the linked list from the ath to the bth node
func (ll *Singly[T]) ReversePartition(left, right int) error {
err := ll.CheckRangeFromIndex(left, right)
if err != nil {
return err
}
tmpNode := &Node[T]{}
tmpNode.Next = ll.Head
pre := tmpNode
for i := 0; i < left-1; i++ {
pre = pre.Next
}
cur := pre.Next
for i := 0; i < right-left; i++ {
next := cur.Next
cur.Next = next.Next
next.Next = pre.Next
pre.Next = next
}
ll.Head = tmpNode.Next
return nil
}
func (ll *Singly[T]) CheckRangeFromIndex(left, right int) error {
if left > right {
return errors.New("left boundary must smaller than right")
} else if left < 1 {
return errors.New("left boundary starts from the first node")
} else if right > ll.length {
return errors.New("right boundary cannot be greater than the length of the linked list")
}
return nil
}
// Display prints out the elements of the list.
func (ll *Singly[T]) Display() {
for cur := ll.Head; cur != nil; cur = cur.Next {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}