fun <T> getCombinedNode(firstList: LinkedNode<T>, secondList: LinkedNode<T>): LinkedNode<T>? {
val fistLength = firstList.length
val secondLength = secondList.length
if (fistLength == 0 || secondLength == 0) return null
var shortNode = if (fistLength <= secondLength) firstList else secondList
var longNode = if (fistLength > secondLength) firstList else secondList
repeat((fistLength - secondLength).absoluteValue) {
longNode = longNode.next!!
}
if (shortNode == longNode) return shortNode
while(shortNode.next != null) {
shortNode = shortNode.next!!
longNode = longNode.next!!
if (shortNode == longNode) return shortNode
}
return null
}
class LinkedNode<T>(var value: T) {
var next: LinkedNode<T>? = null
override fun toString() = "$value[$value -> ${next?.value ?: "null"}]"
}
评论