class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode cur = head;
int count = 0;
while(cur.next != null) {
ListNode temp = cur;
cur =cur.next;
++count;
if(count == 1) {
int i = cur.val;
cur.val = temp.val;
temp.val = i;
count = 0;
}
if(cur.next != null){
cur = cur.next;
}
}
return head;
}
}
24. 两两交换链表中的节点
最新推荐文章于 2023-01-28 22:14:12 发布