var swapPairs = function (head) {
if (!head || !head.next) return head
let cur = head, // 记录当前节点
pre = null // 记录 cur 节点的上一个节点
// 头指针指向第二元素,即交换后的第一个元素
head = head.next
while (cur && cur.next) {
let temp = cur.next // 临时存储待交换的元素,和数组元素交换一个道理
cur.next = temp.next
temp.next = cur
// 如果前驱节点存在,则链接到交换的元素上
if(pre) {
pre.next = temp
}
// 存储下一轮的前驱节点
pre = cur
cur = cur.next
}
return head
};