题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { private lateinit var retCurrent: ListNode /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ fun Merge(pHead1: ListNode?,pHead2: ListNode?): ListNode? { // write code here var point1 = pHead1 var point2 = pHead2 val vRetHead = ListNode(-1) retCurrent = vRetHead while (point1 != null || point2 != null) { val pv1 = point1?.`val` ?: Int.MAX_VALUE val pv2 = point2?.`val` ?: Int.MAX_VALUE if (pv1 > pv2) { point2 = toNext(point2!!) } else { point1 = toNext(point1!!) } } return vRetHead.next } private fun toNext(current: ListNode): ListNode? { val next = current.next retCurrent.next = current retCurrent = current return next } }
第一次学到kotlin还有`name`这种东西....