@@ -2,180 +2,202 @@ package structures
2
2
3
3
/* *
4
4
*
5
- * data structure: singly linked list
5
+ * LinkedList a data structure consisting of a collection of nodes that contain a link to the next/previous node.
6
6
*
7
- * description: in a singly linked list, each element stores a link only to the next element
8
- *
9
- * time to insert an element at the beginning and end of the list: O(1)
10
- * insertion time in the middle by index: O(n)
11
- * delete: O(n)
7
+ * In the single LinkedList each node contains a link only to the next element
12
8
*
13
9
*/
14
10
15
- class SingleLinkedList <T >(
16
- /* *
17
- * stores a reference to the first element of the list
18
- *
19
- * if the list is empty, then the reference is null
20
- */
21
- private var first : Node <T >? = null ,
22
- /* *
23
- * stores a reference to the last element of the list
24
- *
25
- * if the list is empty, then the reference is null
26
- */
27
- private var last : Node <T >? = null ,
28
- ) {
11
+ class SingleLinkedList <T >() {
29
12
30
- /* *
31
- * stores the number of elements in the list
32
- */
33
- private var count: Int = 0
13
+ private var head: Node <T >? = null
14
+ private var tail: Node <T >? = null
15
+
16
+ private var size: Int = 0
17
+
18
+ val isEmpty: Boolean
19
+ get() = head == null
34
20
35
21
/* *
36
- * singly linked list node
37
- *
38
- * @property value - node value
39
- * @property next - link to the next element (assuming the element is not the last one )
22
+ * Complexity:
23
+ * worst time: O(n)
24
+ * best time: O(1)
25
+ * average time: O(n )
40
26
*/
41
- class Node <T >(
42
- private val value : T ,
43
- private var next : Node <T >? = null
44
- ) {
27
+ fun add (index : Int , value : T ) : Boolean {
28
+ if (head == null ) return false
45
29
46
- fun changeNext (next : Node <T >? = null) {
47
- this .next = next
30
+ var i = 0
31
+ var node = head
32
+ var prevNode = head
33
+ while (prevNode != null && node != null ) {
34
+ if (i == index) {
35
+ val newNode = Node (value)
36
+ newNode.changeNext(node)
37
+ prevNode.changeNext(newNode)
38
+ size++
39
+ return true
40
+ }
41
+ i++
42
+ prevNode = node
43
+ node = node.next()
48
44
}
49
45
50
- fun next () = next
51
- fun value () = value
46
+ return false
52
47
}
53
48
49
+ fun add (value : T ) = addLast(value)
50
+
54
51
/* *
55
- * returns the number of elements in the list
52
+ * Complexity:
53
+ * worst time: O(1)
54
+ * best time: O(1)
55
+ * average time: O(1)
56
56
*/
57
- fun size () = count
57
+ fun addFirst (value : T ) {
58
+ val node = Node (value)
59
+ if (head == null ) {
60
+ head = node
61
+ tail = node
62
+ } else {
63
+ node.changeNext(head)
64
+ head = node
65
+ }
66
+ size++
67
+ }
58
68
59
69
/* *
60
- * converts a list into a normal Kotlin list for visual representation and returns it
70
+ * Complexity:
71
+ * worst time: O(1)
72
+ * best time: O(1)
73
+ * average time: O(1)
61
74
*/
62
- fun toList () : List < T > {
63
- if (first == null ) return listOf ( )
64
-
65
- val list = mutableListOf< T >()
66
- var node = first
67
- while (node != null ) {
68
- list.add (node.value() )
69
- node = node.next()
75
+ fun addLast ( value : T ) {
76
+ val node = Node (value )
77
+ if (head == null ) {
78
+ head = node
79
+ tail = node
80
+ } else {
81
+ tail?.changeNext (node)
82
+ tail = node
70
83
}
71
- return list
84
+ size ++
72
85
}
73
86
74
87
/* *
75
- * checks if an element [value] is in the list, returns true if the value exists in the list
88
+ * Complexity:
89
+ * worst time: O(n)
90
+ * best time: O(1)
91
+ * average time: O(n)
76
92
*/
77
93
fun contains (value : T ) : Boolean {
78
- if (first == null ) return false
94
+ if (head == null ) return false
79
95
80
- var node = first
96
+ var node = head
81
97
while (node != null ) {
82
98
if (node.value() == value) {
83
99
return true
84
100
}
85
101
node = node.next()
86
102
}
103
+
87
104
return false
88
105
}
89
106
90
107
/* *
91
- * checks if the list is empty, returns true if the list is empty
92
- */
93
- fun isEmpty () = first == null
94
-
95
- /* *
96
- * removes an element [value] from the list, returns true if the element was successfully removed
108
+ * Complexity:
109
+ * worst time: O(n)
110
+ * best time: O(1)
111
+ * average time: O(n)
97
112
*/
98
113
fun remove (value : T ) : Boolean {
99
- if (first == null ) {
100
- return false
101
- }
114
+ if (head == null ) return false
102
115
103
- var prev = first
104
- var node = first
116
+ var prev = head
117
+ var node = head
105
118
106
119
while (node != null ) {
107
120
if (node.value() == value) {
108
- if (prev?.value() == node.value()) {
109
- this .first = null
110
- this .last = null
121
+ if (head == = node) {
122
+ val oldHead = head
123
+ head = node.next()
124
+ if (oldHead == = tail) {
125
+ tail = head
126
+ }
127
+
128
+ node.changeNext(null )
129
+ node.changeValue(null )
111
130
} else {
112
131
prev?.changeNext(node.next())
113
132
}
114
- count--
133
+
134
+ size--
115
135
return true
116
136
}
117
137
prev = node
118
138
node = node.next()
119
139
}
140
+
120
141
return false
121
142
}
122
143
123
144
/* *
124
- * adds element [value] by index [index], returns true if the element was successfully added at the specified index
145
+ * Complexity:
146
+ * worst time: O(n)
147
+ * best time: O(1)
148
+ * average time: O(n)
125
149
*/
126
- fun add (index : Int , value : T ) : Boolean {
127
-
128
- if (first == null ) return false
129
-
130
- var i = 0
131
- var node = first
150
+ fun clear () {
151
+ var node = head
132
152
while (node != null ) {
133
- if (i == index) {
134
- val newNode = Node (value)
135
- node.changeNext(newNode)
136
- count++
137
- return true
138
- }
139
- i++
153
+ val currentNode = node
154
+
140
155
node = node.next()
156
+
157
+ currentNode.changeNext(null )
158
+ currentNode.changeValue(null )
141
159
}
142
160
143
- return false
161
+ head = null
162
+ tail = null
163
+ size = 0
144
164
}
145
165
146
- /* *
147
- * similar addLast method
148
- */
149
- fun add ( value : T ) = addLast(value )
166
+ override fun toString (): String {
167
+ val builder = StringBuilder ()
168
+ builder.append( " size: $size \n " )
169
+ builder.append( " elements: " )
150
170
151
- /* *
152
- * adds an element [value] to the beginning of the list
153
- */
154
- fun addFirst (value : T ) {
155
- val node = Node (value)
156
- if (first == null ) {
157
- this .first = node
158
- this .last = node
159
- } else {
160
- node.changeNext(first)
161
- this .first = node
171
+ var node = head
172
+ var next = node?.next()
173
+ while (node != null && next != null ) {
174
+ builder.append(" ${node.value()} , " )
175
+
176
+ node = node.next()
177
+ next = node?.next()
162
178
}
163
- count++
179
+ if (node != null ) {
180
+ builder.append(node.value())
181
+ }
182
+
183
+ return builder.toString()
164
184
}
165
185
166
- /* *
167
- * adds an element [value] to the end of the list
168
- */
169
- fun addLast (value : T ) {
170
- val newNode = Node (value)
171
- if (first == null ) {
172
- this .first = newNode
173
- this .last = newNode
174
- } else {
175
- this .last?.changeNext(newNode)
176
- this .last = newNode
186
+ class Node <T >(
187
+ private var value : T ? = null ,
188
+ private var next : Node <T >? = null
189
+ ) {
190
+
191
+ fun next () = next
192
+ fun changeNext (node : Node <T >? = null) {
193
+ next = node
177
194
}
178
- count++
195
+
196
+ fun value () = value
197
+ fun changeValue (newValue : T ? ) {
198
+ value = newValue
199
+ }
200
+
179
201
}
180
202
181
203
}
0 commit comments