思路
使用一个节点一直位于交换的两个节点的前面一个节点即可
代码
class Solution {fun swapPairs(head: ListNode?): ListNode? {if (head?.next == null) return headval tmpHead = ListNode(0)var curr = tmpHeadvar firstN = headvar secondN = head.nextwhile (firstN!!.next != null) {firstN.next = secondN!!.nextsecondN.next = firstNcurr!!.next = secondNcurr = firstNif (firstN.next != null) {firstN = firstN.nextsecondN = firstN!!.next}}return tmpHead.next}}
