-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListImpl.kt
206 lines (186 loc) · 5.19 KB
/
LinkedListImpl.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
200
201
202
203
204
205
206
package com.ds.implementation.linkedlist.singly
import com.ds.implementation.linkedlist.common.LinkedListOperation
import com.ds.implementation.linkedlist.common.Node
class LinkedListImpl<T>(private var head: ListNode<T>?) : LinkedListOperation<T> {
override fun insert(t: T): Boolean {
if (head == null) {
head = ListNode(t)
} else {
val current = head
head = ListNode(t)
head?.next = current
}
return true
}
override fun insertAtPosition(t: T, position: Int): Boolean {
if (position == -1) return false
if (position == 0 || head == null) {
return insert(t)
}
var index = 0
var current = head
val iNode = ListNode(t)
var prev = head
while (current != null) {
if (position == index) {
prev?.next = iNode
iNode.next = current
return true
}
prev = current
current = current.next
index++
}
prev?.next = iNode
return true
}
override fun insertAfterNode(t: T, node: Node<T>): Boolean {
if (head == null) {
return insert(t)
}
var index = 0
var current = head
val iNode = ListNode(t)
var prev = head
while (current != null) {
if (node.value == current.value) {
val next = current.next
current.next = iNode
iNode.next = next
return true
}
prev = current
current = current.next
index++
}
prev?.next = iNode
return false
}
override fun searchByValue(t: T): Int {
var current = head
var index = 0
while (current != null) {
if (t == current.value) {
return index
}
index++
current = current.next
}
return -1
}
override fun searchByNode(node: Node<T>): Int {
return searchByValue(node.value)
}
override fun delete(): Boolean {
if (head == null) return false
val newHead = head?.next
head = null
head = newHead
return true
}
override fun deleteAtPosition(position: Int): Boolean {
if (position == -1) return false
if (position == 0) delete()
var index = 0
var current = head
var prev = head
while (current != null) {
if (position == index) {
prev?.next = current.next
return true
}
prev = current
current = current.next
index++
}
return false
}
override fun deleteAfterNode(node: Node<T>): Boolean {
var current = head
while (current != null) {
if (node.value == current.value) {
current.next = current.next?.next
return true
}
current = current.next
}
return false
}
override fun deleteNode(node: Node<T>): Boolean {
if (head == null) return false
if (node.value == head?.value) {
delete()
return true
}
var current = head
var prev = head
while (current != null) {
if (current.value == node.value) {
prev?.next = current.next
return true
}
prev = current
current = current.next
}
return false
}
override fun nodeAtIndex(index: Int): Node<T>? {
var current = head
var counter = 0
while (current != null) {
if (counter == index) {
return current
}
counter++
current = current.next
}
return null
}
override fun reverse() {
var current = head
var prev: ListNode<T>? = null
var next: ListNode<T>? = null
while (current != null) {
next = current.next
current.next = prev
prev = current
current = next
}
head = prev
}
fun palindrome(){
var current = head
var c = ListNode(head?.value)
var prev: ListNode<T>? = null
var next: ListNode<T>? = null
while (current?.next != null) {
var newNode = ListNode(current.next!!.value)
newNode.next = prev
prev = newNode
current = next
}
var valid = head
var reverse = c
var isPalindrome = true
while (reverse!=null && valid != null)
{
if(reverse.value != valid.value)
{
isPalindrome = false
}
reverse = reverse.next!!
valid = valid.next!!
}
println("Is Palindrome $isPalindrome")
}
override fun print(msg: String?) {
msg?.let { println(it) }
var current = head
do {
kotlin.io.print("|${current?.value}|->")
current = current?.next
} while (current != null)
kotlin.io.print("X")
println()
}
}