I ran across this and was going to reply some optimization to that code, but it occurred to me that the real issue is an incomplete design. In reality you should be using immutable data structures, not mutable ones, but I will stick with mutable.
What you need is this method:
operator fun SLLNode?.plusAssign(operand: Int): SLLNode? =
when {
operand == 0 -> this
else -> this?.also { value += operand } ?: SLLNode(operand)
}
Then your original code just becomes:
fun SLLNode?.sumListWith(node: SLLNode?, carry: Int = 0): SLLNode? =
when {
this == null -> node += carry
node == null -> this += carry
else -> {
...
}
}