Merge pull request #134 from KyrieChang/patch-4

添加 0024.两两交换链表中的节点.md Java递归版本
This commit is contained in:
Carl Sun 2021-05-15 17:46:20 +08:00 committed by GitHub
commit 8c3d8aab25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 0 deletions

View File

@ -86,6 +86,26 @@ 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;
}
}
```
```java
// 虚拟头结点
class Solution {