-
Notifications
You must be signed in to change notification settings - Fork 109
/
SingleLinkedList.kt
199 lines (163 loc) · 4.1 KB
/
SingleLinkedList.kt
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package structures
/**
*
* LinkedList a data structure consisting of a collection of nodes that contain a link to the next/previous node.
*
* In the single LinkedList each node contains a link only to the next element
*
*/
class SingleLinkedList<T>() {
private var head: Node<T>? = null
private var tail: Node<T>? = null
private var size: Int = 0
val isEmpty: Boolean
get() = head == null
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun add(index: Int, value: T) : Boolean {
if (head == null) return false
var i = 0
var node = head
var prevNode = head
while (prevNode != null && node != null) {
if (i == index) {
val newNode = Node(value)
newNode.changeNext(node)
prevNode.changeNext(newNode)
size++
return true
}
i++
prevNode = node
node = node.next()
}
return false
}
fun add(value: T) = addLast(value)
/**
* Complexity:
* worst time: O(1)
* best time: O(1)
* average time: O(1)
*/
fun addFirst(value: T) {
val node = Node(value)
if (head == null) {
head = node
tail = node
} else {
node.changeNext(head)
head = node
}
size++
}
/**
* Complexity:
* worst time: O(1)
* best time: O(1)
* average time: O(1)
*/
fun addLast(value: T) {
val node = Node(value)
if (head == null) {
head = node
tail = node
} else {
tail?.changeNext(node)
tail = node
}
size++
}
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun contains(value: T) : Boolean {
if (head == null) return false
var node = head
while (node != null) {
if (node.value() == value) {
return true
}
node = node.next()
}
return false
}
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun remove(value: T) : Boolean {
if (head == null) return false
var previous: Node<T>? = null
var node = head
while (node != null) {
if (node.value() == value) {
val nextNode = node.next()
previous?.changeNext(nextNode)
if (head === node) {
head = nextNode
}
if (tail === node) {
tail = previous
}
node.changeNext(null)
node.changeValue(null)
size--
return true
}
previous = node
node = node.next()
}
return false
}
// Complexity: O(n)
fun clear() {
var node = head
while (node != null) {
val currentNode = node
node = node.next()
currentNode.changeNext(null)
currentNode.changeValue(null)
}
head = null
tail = null
size = 0
}
override fun toString(): String {
val builder = StringBuilder()
builder.append("size: $size\n")
builder.append("elements: ")
var node = head
while (node != null) {
builder.append(node.value())
// it's necessary to see the correct node connections
if (node.next() != null) {
builder.append(" - ")
}
node = node.next()
}
return builder.toString()
}
class Node<T>(
private var value: T? = null,
private var next: Node<T>? = null
) {
fun next() = next
fun changeNext(node: Node<T>? = null) {
next = node
}
fun value() = value
fun changeValue(newValue: T?) {
value = newValue
}
}
}