1. var swapPairs = function (head) {
    2. if (!head || !head.next) return head
    3. let cur = head, // 记录当前节点
    4. pre = null // 记录 cur 节点的上一个节点
    5. // 头指针指向第二元素,即交换后的第一个元素
    6. head = head.next
    7. while (cur && cur.next) {
    8. let temp = cur.next // 临时存储待交换的元素,和数组元素交换一个道理
    9. cur.next = temp.next
    10. temp.next = cur
    11. // 如果前驱节点存在,则链接到交换的元素上
    12. if(pre) {
    13. pre.next = temp
    14. }
    15. // 存储下一轮的前驱节点
    16. pre = cur
    17. cur = cur.next
    18. }
    19. return head
    20. };