[leetcode]24. Swap Nodes in Pairs -- JavaScript 代码

来源:互联网 发布:server name 多个域名 编辑:程序博客网 时间:2024/06/07 10:34
/** * Definition for singly-linked list. * function ListNode(val) { *     this.val = val; *     this.next = null; * } *//** * @param {ListNode} head * @return {ListNode} */var swapPairs = function(head) {    if(head===null || head.next===null) return head;    var tmp = head;    head = head.next;    var thelast = null;    while(tmp!==null && tmp.next!==null){        next = tmp.next;        next2 = next.next;        next.next = tmp;        tmp.next = next2;        if(thelast!==null){            thelast.next = next;        }        thelast = tmp;        tmp = next2;    }    return head;};
0 0
原创粉丝点击