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