您的位置:首页 > Web前端 > Node.js

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

2016-08-18 15:04 525 查看
/**
* 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;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript leetcode