/**
* 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;
};
[leetcode]24. Swap Nodes in Pairs -- JavaScript 代码
最新推荐文章于 2021-12-19 15:12:12 发布