You are currently viewing If the Linked List is empty so the tail should also not be there then why we make it null.

If the Linked List is empty so the tail should also not be there then why we make it null.

Hello Kotlins, I’m new on Reddit and a newbie in kotlin just started learning Data Structures and Algorithms using Kotlin. I made a Linked List where I have to make a pop function for removing the head and making the next node, the head. I doing it all from the book but in the book, they also write some lines of code in the pop function that if the list becomes empty so we will make the tail null. But why we supposed to make the tail null as the empty list doesn’t even contain the tail. Here’s the code…

fun pop():T{
//if list is not empty then dec size
if(!isEmpty()) size--
/*simply storing the value to be pop in a result and making the next node as head*/
val result = head?.nodeValue
head = head?.nextNode
/*if list become empty then we will make tail null*/

from here I cannot understand
if (isEmpty()){
tail = null
}
return result!!
}

submitted by /u/Winter-Protection-62
[link] [comments]