class Solution {
public ListNode swapPairs(ListNode head) {
// 判断特例, 使用递归的
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
// 两两交换链表中的节点
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}
LeetCode 24两两交换链表中的节点(JAVA)
最新推荐文章于 2022-05-16 22:59:47 发布