mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加 24两两交换链表中的节点 Kotlin实现
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user