https://kotlinlang.org logo
#getting-started
Title
# getting-started
o

oday

07/12/2021, 12:29 PM
I’m following this code along with the debugger, and I’m very confused as to when or why
head
ends up changing and pointing to new elements https://paste.ofcode.org/3h6juagTuPwvZaDEeTkwCP
when an element is found to be inside the elements set, that element is removed from the list
like why does reassigning
current
also reassign
head
if I assign head to something, that something is now head?
so its pass by value
s

Sourabh Rawat

07/12/2021, 3:09 PM
can you provide an input where
head
is getting reassigned?
o

oday

07/12/2021, 3:11 PM
yea any list of
Node(2, Node(3, Node(4, Node(5))))
it ends up reversing the list correctly
how does head come out changed in the end? its really confusing
s

Sourabh Rawat

07/12/2021, 3:25 PM
https://pl.kotl.in/FTMbmo_3a isnt this expected?
o

oday

07/12/2021, 3:27 PM
sure, but inside the function I am expecting to at least see some line that assigns current back to head ? I dunno why to expect that but I guess this is because function parameters are always val so you need to grab a reference to them to work with it
is this not the same as above?
Copy code
fun main(){
   val variable = 100
   println(test(variable))
}

fun test(testValue: Int): Int {
    var temp = testValue

    temp = 98

    return testValue
}
result should be 98 now
took the value of testValue assigned it to a temp, changed the temp, returned testvalue
s

Sourabh Rawat

07/12/2021, 3:38 PM
Ill try to explain. Might not be the best explanation.
head
will never be changed, although it's
next
value can be changed(in the while loop). Since for removing duplicates you won't be removing the first element, only second and so on. thats why they are just returning head. In fact, they don't even need to return the
head
from the function, as it will always refer to same node object as the passed
head
to the function.
o

oday

07/12/2021, 3:41 PM
aha!
and yes that is true about not needing to return
head
, I added that myself because I thought how come it looks like a no-op
ok thanks a lot