Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

两两交换链表中的节点

Sunny-117 opened this issue · comments

commented
两两交换链表中的节点
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function(head) {
    if(!head || !head.next){
        return head;
    }
    const newHead = head.next; //第二个节点
    head.next = swapPairs(newHead.next); //把第三个节点传入,返回的是第四个节点
    newHead.next = head;//第一个节点接在第二节点之后
    return newHead
};