Update 0024.两两交换链表中的节点.md

0024.两两交换链表中的节点 Java递归版本
This commit is contained in:
kyrie
2021-05-15 12:43:12 +08:00
committed by GitHub
parent db785168ba
commit 66cd701270

View File

@ -86,6 +86,23 @@ public:
Java
```Java
class Solution {
public ListNode swapPairs(ListNode head) {
// base case 退出提交
if(head == null || head.next == null) return head;
// 获取当前节点的下一个节点
ListNode next = head.next;
// 进行递归
ListNode newNode = swapPairs(next.next);
// 这里进行交换
next.next = head;
head.next = newNode;
return next;
}
}
```
Python