/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ classSolution { funmergeTwoLists(l1: ListNode?, l2: ListNode?): ListNode? { if (l1 == null) { return l2 } if (l2 == null) { return l1 } var list1 = l1 var list2 = l2
var first = ListNode(0) var result = first while (list1 != null && list2 != null) { if (list1.`val` > list2.`val`) { result.next = list2 list2.next.also { list2 = it } } else { result.next = list1 list1 = list1.next } result = result.next }