From 66cd7012705cfabf2604329bcdc7a34a12541797 Mon Sep 17 00:00:00 2001 From: kyrie <783842766@qq.com> Date: Sat, 15 May 2021 12:43:12 +0800 Subject: [PATCH] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0024.两两交换链表中的节点 Java递归版本 --- problems/0024.两两交换链表中的节点.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 9f07b4f7..58cd3d8a 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -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: