Skip to content

Commit a751692

Browse files
author
ivan
committed
impl deleteLastKthNode and test
1 parent f23c5ab commit a751692

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

scala/src/main/scala/ch07_linkedlist/LinkedListAlgo.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ object LinkedListAlgo {
8989
head
9090
}
9191

92+
def deleteLastKthNode(headOpt: Option[Node], k: Int): Option[Node] = {
93+
require(k > 0, "k must greater than 0")
94+
headOpt match {
95+
case None => None
96+
case Some(head) =>
97+
var index = 0
98+
var slow = headOpt
99+
var fast = headOpt
100+
while (fast.get.next.isDefined && index < k) {
101+
//move fast cursor to k
102+
fast = fast.get.next
103+
index += 1
104+
}
105+
106+
if (fast.get.next.isEmpty && index + 1 == k) {
107+
//deleting the head element
108+
return head.next
109+
}
110+
111+
require(index.equals(k), "given linked list should contains at least k elements ")
112+
while (fast.get.next.isDefined) {
113+
fast = fast.get.next
114+
slow = slow.get.next
115+
}
116+
117+
//fast cursor is in the end of the chain
118+
//slow is the previous pos of k element
119+
//do the operation
120+
slow.get.next = slow.get.next.get.next
121+
}
122+
123+
headOpt
124+
}
125+
92126
//form all the chain value as string
93127
def mkStringForChain(node: Node): String = {
94128
val result = new StringBuilder

scala/src/test/scala/ch07_linkedlist/LinkedListAlgoTest.scala

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,73 @@ class LinkedListAlgoTest extends FlatSpec with Matchers {
136136
assert(node.isEmpty)
137137
}
138138

139+
it should "delete last 2nd element " in {
140+
val list = new SinglyLinkedList()
141+
142+
for (i <- 0 to 9) {
143+
list.insertTail(i)
144+
}
145+
LinkedListAlgo.mkStringForChain(list.headOpt.get) should equal((0 to 9).toArray.mkString(""))
146+
147+
val node = LinkedListAlgo.deleteLastKthNode(list.headOpt, 2)
148+
assert(node.isDefined)
149+
LinkedListAlgo.mkStringForChain(node.get) should equal("012345679")
150+
}
151+
152+
it should "delete last element " in {
153+
val list = new SinglyLinkedList()
154+
155+
for (i <- 0 to 9) {
156+
list.insertTail(i)
157+
}
158+
LinkedListAlgo.mkStringForChain(list.headOpt.get) should equal((0 to 9).toArray.mkString(""))
159+
160+
val node = LinkedListAlgo.deleteLastKthNode(list.headOpt, 1)
161+
assert(node.isDefined)
162+
LinkedListAlgo.mkStringForChain(node.get) should equal("012345678")
163+
}
164+
165+
it should "delete first element " in {
166+
val list = new SinglyLinkedList()
167+
168+
for (i <- 0 to 9) {
169+
list.insertTail(i)
170+
}
171+
LinkedListAlgo.mkStringForChain(list.headOpt.get) should equal((0 to 9).toArray.mkString(""))
172+
173+
val node = LinkedListAlgo.deleteLastKthNode(list.headOpt, 10)
174+
assert(node.isDefined)
175+
LinkedListAlgo.mkStringForChain(node.get) should equal("123456789")
176+
}
177+
178+
it should "delete firs only element " in {
179+
val list = new SinglyLinkedList()
180+
list.insertTail(0)
181+
182+
val node = LinkedListAlgo.deleteLastKthNode(list.headOpt, 1)
183+
assert(node.isEmpty)
184+
}
185+
186+
it should "throw exception if k < 0 " in {
187+
val list = new SinglyLinkedList()
188+
189+
for (i <- 0 to 9) {
190+
list.insertTail(i)
191+
}
192+
assertThrows[IllegalArgumentException] {
193+
LinkedListAlgo.deleteLastKthNode(list.headOpt, -1)
194+
}
195+
}
196+
197+
it should "throw exception if k greater than list length " in {
198+
val list = new SinglyLinkedList()
199+
200+
for (i <- 0 to 9) {
201+
list.insertTail(i)
202+
}
203+
assertThrows[IllegalArgumentException] {
204+
LinkedListAlgo.deleteLastKthNode(list.headOpt, 15)
205+
}
206+
}
207+
139208
}

0 commit comments

Comments
 (0)