class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy=new ListNode(-1,head);
ListNode n0=dummy;
ListNode n1=head;
if(head == null || head.next == null) return head;
while(n1!=null && n1.next!=null){
ListNode n2=n1.next;
ListNode n3=n2.next;
n0.next=n2;
n2.next=n1;
n1.next=n3;
n0=n1;
n1=n3;
}
return dummy.next;
}
}
Leetcode24:两两交换链表中的节点
于 2023-12-29 22:09:53 首次发布