From acc3ac725c44e880cd691c23e09dff94b8eda6e9 Mon Sep 17 00:00:00 2001 From: whai Date: Fri, 29 Dec 2023 20:18:12 +0800 Subject: [PATCH] =?UTF-8?q?LeetCode24=20-=20=E9=93=BE=E8=A1=A8=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E5=AF=B9=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/middle/LeetCode24.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/middle/LeetCode24.java diff --git a/src/main/java/cn/whaifree/leetCode/middle/LeetCode24.java b/src/main/java/cn/whaifree/leetCode/middle/LeetCode24.java new file mode 100644 index 0000000..3aa792b --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/middle/LeetCode24.java @@ -0,0 +1,113 @@ +package cn.whaifree.leetCode.middle; + +import org.junit.Test; +import cn.whaifree.leetCode.model.ListNode; + +/** + * 两两交换链表中的节点 + * 中等 + * 相关标签 + * 相关企业 + * 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 + * + * 示例 1: + * 输入:head = [1,2,3,4] + * 输出:[2,1,4,3] + * + * 示例 2: + * 输入:head = [] + * 输出:[] + * + * 示例 3: + * 输入:head = [1] + * 输出:[1] + * + */ +public class LeetCode24 { + + /** + * 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; } + * } + * pre index tmp + * 0 -> 1 -> 2 -> 3 -> 4 -> 5 + * + * pre index tmp + * 0 1 -> 2 -> 3 -> 4 -> 5 + * ----------^ + * + * ----------- + * pre index tmp | + * 0 1 -> 2 -> 3 -> 4 -> 5 + * ----------^ + * + * ----------- + * pre index tmp | + * 0 1 <- 2 -> 3 -> 4 -> 5 + * ----------^ + * + * pre tmp index + * 0-> 2 -> 1 -> 3 -> 4 -> 5 + * + * + * pre tmp index + * 0-> 2 -> 1 -> 3 -> 4 -> 5 + */ + public ListNode swapPairs(ListNode head) { + if (head == null) { + return head; + } + ListNode pre = new ListNode(0, head); + ListNode tmp = head.next; + ListNode index = head; + head = pre; + while (tmp != null) { + pre.next = tmp; + index.next = tmp.next; + tmp.next = index; + pre = index; + index = pre.next; + tmp = index == null ? null : index.next; + } + return head.next; + } + + public ListNode swapPairsD(ListNode head) { + if (head == null) { + return head; + } + return swap(head, head.next); + } + + /** + * 递归的关键: + * 1. 结束条件 + * 2. 递归条件(只考虑当前这一点点的情况,剩下的交给下次递归) + * @param pre + * @param after + * @return + */ + public ListNode swap(ListNode pre, ListNode after) { + if (pre == null || after == null) { + return pre; + } + // 递归后面的指针 + pre.next = swap(after.next, after.next == null ? null : after.next.next); + after.next = pre; + + return after; + } + + + @Test + public void test() { + ListNode listNode = swapPairsD(ListNode.listNodeFromArray(new int[]{1,2,3,4,5})); + ListNode.printList(listNode); + + } +}