LeetCode24

- 链表两两对换
This commit is contained in:
whai 2023-12-29 20:18:12 +08:00
parent f6d0c1582a
commit acc3ac725c

View File

@ -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);
}
}