https://leetcode.cn/problems/swap-nodes-in-pairs/description/
1234567891011121314151617
var swapPairs = function(head) { // 创建虚拟结点,从虚拟结点开始,每次处理下面的两个结点。然后向后跳转两个结点。 let dummy = new ListNode(0) dummy.next = head //prev是当前结点 let prev = dummy while(prev.next && prev.next.next){ let node1 = prev.next let node2 = prev.next.next //整个交换逻辑在这里 prev.next = node2 node1.next = node2.next node2.next = node1 prev = node1 } return dummy.next};