oday
07/01/2021, 5:00 PModay
07/01/2021, 5:01 PMreverseIterative()
is changing the Node object for some reason..Klitos Kyriacou
07/01/2021, 5:14 PMval
instead of var
and see where it complains.oday
07/01/2021, 5:19 PModay
07/01/2021, 5:19 PModay
07/01/2021, 5:21 PModay
07/01/2021, 5:22 PMvalue
and next
Klitos Kyriacou
07/01/2021, 5:22 PModay
07/01/2021, 5:23 PMcurrent.next = prev
will complainoday
07/01/2021, 5:23 PModay
07/01/2021, 5:30 PModay
07/01/2021, 5:30 PModay
07/01/2021, 6:21 PMnode.next?.next = node
when next
is now a val
this is the code for this one
https://paste.ofcode.org/umxk2HQsUHM7XGMCs6phK7oday
07/01/2021, 6:30 PMRoukanken
07/01/2021, 7:45 PMif (head?.next != null)
return head
you should return when it is null, same as before
b) head = head?.next.copy(next = head)
this line does NOT do what you think it does:
it's same as head = (head?.next).copy(next = head)
which means you will assign modified copy of head?.next
to head, and not copy of head
c) if you look how to fix b)
you notice that you can't really easily do "add head at last of the list returned" without a lot of overhead - you would likely need a helper function to return 2 nodes: start and end, and then just forget the one on top-level functionoday
07/01/2021, 10:25 PModay
07/01/2021, 10:28 PModay
07/01/2021, 10:28 PModay
07/01/2021, 10:28 PMhead.next = somethingElse
when working with LinkedLists..Roukanken
07/02/2021, 6:28 AModay
07/02/2021, 6:59 AMRoukanken
07/02/2021, 7:03 AMKlitos Kyriacou
07/02/2021, 8:41 AM.next
assigned to the existing list. For most other modifications, maybe you're right.Roukanken
07/02/2021, 8:45 AModay
07/02/2021, 9:06 AModay
07/02/2021, 9:06 AMRoukanken
07/02/2021, 9:25 AModay
07/02/2021, 11:48 AModay
07/02/2021, 11:49 AMRoukanken
07/02/2021, 12:19 PModay
07/02/2021, 12:44 PModay
07/02/2021, 12:51 PMRoukanken
07/02/2021, 12:53 PModay
07/02/2021, 12:53 PModay
07/02/2021, 12:54 PMRoukanken
07/02/2021, 12:55 PMRoukanken
07/02/2021, 1:03 PModay
07/02/2021, 1:52 PMcurrent
to the next and so on https://github.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/blob/master/Chapter03/LinkyList.ktoday
07/02/2021, 2:11 PMAt this point, you've defined an interface for a linked list that most programmers around the world can relate to. However, there's work to be done to adorn the Kotlin semantics. In the next half of the chapter, you'll focus on making the interface better by bringing it closer to idiomatic Kotlin.
oday
07/02/2021, 2:13 PModay
07/02/2021, 2:22 PMRoukanken
07/02/2021, 2:25 PMclass LinkedList<T> {
// TODO
}
val data: LinkedList<Int> = TODO()
for (item: Int in data) {
println(item)
}
Kotlin will not allow you to do this - it does not know how to get and fill the item
variable from the data
structure
This will throw you an For-loop range must have an 'iterator()' method
erroroday
07/02/2021, 2:27 PModay
07/02/2021, 2:27 PMRoukanken
07/02/2021, 2:28 PMRoukanken
07/02/2021, 2:29 PMIterable<T>
forces you to implement the iterator()
method, and when you do that, then the for cycle above will work properlyRoukanken
07/02/2021, 2:33 PMIterable<Int>.sum(): Int
which would mean that this would work too: (ofc after being implemented properly, instead of TODO)
class LinkedList<T>: Iterable<T> {
// TODO
override fun iterator(): Iterator<T> {
TODO()
}
}
val data: LinkedList<Int> = TODO()
println(data.sum())
as the implementation of sum()
function does not need to know how structure works, only be able to know it's contents (handled by Iterable<T>
) and be able to sum it's items (it's collection of Int
so it can sum)