title: Kotlin, remove duplicates from Linkedlist : smart cast is impossible
body: <p>I implemented a LinkedList in kotlin and wrote a method to remove duplicates from it:</p>
  <pre><code>class Node (value:Int) {
      var value = value
      var next:Node? = null
  
  fun addNodeToTail(value:Int){
      var node = this
      while (node.next != null) {
          node = node.next
      }
      val newNode= Node(value)
      node.next= newNode
  
  }
  fun removeDuplicates (){
  
      val set = HashSet&lt;Int&gt;()
      var node = this
      set.add(node.value)
      while(node.next != null){
          if (set.contains(node.next?.value)){
              node.next= node.next?.next
          }else{
              set.add(node.next.value)
              node= node.next
          }
      }
  }
  }
  </code></pre>
  <p>In the last two lines:</p>
  <pre><code> set.add(node.next.value)
   node= node.next
  </code></pre>
  <p>(and in the addNodeToTail method), the compiler says that smart cast is impossible because of complex expression. I have to add non-null asserted call (!!).</p>
  <p>I want to understand why this solution is not accepted, although the while expression checks that node.next is not null. And I want to know if there is a better solution than using non-null asserted call (!!).</p>
  <p>Thank you for your help</p>