Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd56ccc

Browse files
refactored SingleLinkedList
1 parent 2b31d43 commit fd56ccc

File tree

2 files changed

+283
-152
lines changed

2 files changed

+283
-152
lines changed

‎src/main/kotlin/structures/SingleLinkedList.kt

Lines changed: 131 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,180 +2,202 @@ package structures
22

33
/**
44
*
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.
66
*
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
128
*
139
*/
1410

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>() {
2912

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
3420

3521
/**
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)
4026
*/
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
4529

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()
4844
}
4945

50-
fun next() = next
51-
fun value() = value
46+
return false
5247
}
5348

49+
fun add(value: T) = addLast(value)
50+
5451
/**
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)
5656
*/
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+
}
5868

5969
/**
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)
6174
*/
62-
fun toList() : List<T> {
63-
if (first ==null) returnlistOf()
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
7083
}
71-
return list
84+
size++
7285
}
7386

7487
/**
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)
7692
*/
7793
fun contains(value: T) : Boolean {
78-
if (first == null) return false
94+
if (head == null) return false
7995

80-
var node = first
96+
var node = head
8197
while (node != null) {
8298
if (node.value() == value) {
8399
return true
84100
}
85101
node = node.next()
86102
}
103+
87104
return false
88105
}
89106

90107
/**
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)
97112
*/
98113
fun remove(value: T) : Boolean {
99-
if (first == null) {
100-
return false
101-
}
114+
if (head == null) return false
102115

103-
var prev = first
104-
var node = first
116+
var prev = head
117+
var node = head
105118

106119
while (node != null) {
107120
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)
111130
} else {
112131
prev?.changeNext(node.next())
113132
}
114-
count--
133+
134+
size--
115135
return true
116136
}
117137
prev = node
118138
node = node.next()
119139
}
140+
120141
return false
121142
}
122143

123144
/**
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)
125149
*/
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
132152
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+
140155
node = node.next()
156+
157+
currentNode.changeNext(null)
158+
currentNode.changeValue(null)
141159
}
142160

143-
return false
161+
head = null
162+
tail = null
163+
size = 0
144164
}
145165

146-
/**
147-
* similar addLast method
148-
*/
149-
funadd(value:T) = addLast(value)
166+
overridefuntoString(): String {
167+
val builder =StringBuilder()
168+
builder.append("size: $size\n")
169+
builder.append("elements: ")
150170

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()
162178
}
163-
count++
179+
if (node != null) {
180+
builder.append(node.value())
181+
}
182+
183+
return builder.toString()
164184
}
165185

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
177194
}
178-
count++
195+
196+
fun value() = value
197+
fun changeValue(newValue: T?) {
198+
value = newValue
199+
}
200+
179201
}
180202

181203
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /