24.两两交换链表
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function(head) {
let dummy = new ListNode();
dummy.next = head;
let current = dummy;
while(current.next !==null &¤t.next.next !==null){
let n1 = current.next;
let n2 = current.next.next;
current.next = n2;
n1.next=n2.next;
n2.next = n1;
current=n1;
}
return dummy.next;
};
最后
以上就是隐形戒指最近收集整理的关于力扣(四)的全部内容,更多相关力扣(四)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复