/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head ==null||head.next==null) return head;
ListNode d=new ListNode(0);
ListNode next=head.next; //2
d.next=next; //dummy直接连到2上
head.next=next.next;
next.next=head;
head.next=swapPairs(head.next) ; //2->1->3 此时head=1,head.next=3;
// ListNode newHead=d.next;
// d.next=null;
return d.next;
}
}
LeetCode 24. 两两交换链表中的节点
最新推荐文章于 2023-07-20 14:47:14 发布