添加 24两两交换链表中的节点 Kotlin实现

This commit is contained in:
wz-mibookwindows
2021-08-07 08:28:16 +08:00
parent a7fa0055ac
commit cb16c9fe18

View File

@ -228,6 +228,27 @@ var swapPairs = function (head) {
};
```
Kotlin:
```kotlin
fun swapPairs(head: ListNode?): ListNode? {
val dummyNode = ListNode(0).apply {
this.next = head
}
var cur: ListNode? = dummyNode
while (cur?.next != null && cur.next?.next != null) {
val temp = cur.next
val temp2 = cur.next?.next?.next
cur.next = cur.next?.next
cur.next?.next = temp
cur.next?.next?.next = temp2
cur = cur.next?.next
}
return dummyNode.next
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)