defsolution(first: ListNode, second: ListNode): ListNode = { if (first == null) return second if (second == null) return first var cur = if (first.value > second.value) second else first val head = cur var i = if (first.value > second.value) first else first.next var j = if (first.value > second.value) second.next else second while (i != null && j != null) { if (i.value > j.value) { cur.next = j j = j.next } else { cur.next = i i = i.next } cur = cur.next } while (i != null) { cur.next = i cur = cur.next i = i.next }
while (j != null) { cur.next = j cur = cur.next j = j.next }
head }
caseclassListNode(value: Int, var next: ListNode) }