Plantre Plantre
首页
后端
技术
硬件
  • 前端文章

    • HTML
    • CSS
    • JavaScript
  • 技术

    • 技术文档
    • GitHub技巧
    • Nodejs
    • 博客搭建
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

plantre

一个后端开发者
首页
后端
技术
硬件
  • 前端文章

    • HTML
    • CSS
    • JavaScript
  • 技术

    • 技术文档
    • GitHub技巧
    • Nodejs
    • 博客搭建
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 计算机组成原理

  • 操作系统

  • 计算机网络

  • 设计模式

  • Java

  • Spring

  • SpringCloud

  • MySQL

  • Redis

  • 分布式

  • Zookeeper

  • Dubbo

  • Kafka

  • 数据结构

  • 算法

  • OJ

    • 自定义
    • 排序
    • 位运算
    • 二分查找
    • 递归
      • 约瑟夫环
        • 206. 反转链表 - 力扣(LeetCode)
        • 25. K 个一组翻转链表 - 力扣(LeetCode)
        • 23. 合并 K 个升序链表 - 力扣(LeetCode)
    • 双指针(逆向,快慢)
    • 滑动窗口
    • 辅助栈
    • 贪心
    • 回溯
    • 动态规划
    • 二叉树
    • DFS
    • 拼接&拆分
  • 从道家哲学看计算机?
  • 后端
  • OJ
plantre
2025-06-11
目录

递归

# 约瑟夫环 (opens new window)

public class Josephus {
// 􀨧􀔎􁭓􀭭􀚍􀷄
public static int f(int n, int k) {
    // 􀦇􀺎􀝝􀹍􀓞􀓻􀕈􀒅􀚞􁬬􀢧 1
    if (n == 1) {
       return 1;
    }
    return (f(n - 1, k) + k - 1) % n + 1;
}

public static void main(String[] args) {
    int n = 10;
    int k = 3;
    System.out.println("最后留下的那个人的编号是:" + f(n, k));
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 206. 反转链表 - 力扣(LeetCode) (opens new window)

class Solution {
    public ListNode reverseList(ListNode head) {
            if(head==null) return head;
            if(head.next==null) return head;
            ListNode h=reverseList(head.next);
            ListNode t=h;
            while(t.next!=null) t=t.next;
            t.next=head;
            head.next=null;
            return h;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

优化后

image-20250611233846247

class Solution {
    public ListNode reverseList(ListNode head) {
            if(head==null) return head;
            if(head.next==null) return head;
            ListNode h = reverseList(head.next);//执行完这句后,head.next是最后一个节点了,head.next后面的节点应该是head,head是最后一个节点
            // head.next是第二个节点
            head.next.next = head;
            head.next = null;
            return h;
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 25. K 个一组翻转链表 - 力扣(LeetCode) (opens new window)

充分利用reverseKGroup()的定义

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null) return null;
        // 区间 [a, b) 包含 k 个待反转元素 
        ListNode a, b;
        a = b = head;
        for (int i = 0; i < k; i++) {
             // 不⾜ k 个,不需要反转,base case 
            if (b == null) return head;
            b = b.next;
        }
        // 反转前 k 个元素 
        ListNode newHead = reverse(a, b);
        a.next = reverseKGroup(b, k);
        return newHead;
    }
    /* 反转区间 [a, b) 的元素,注意是左闭右开 */ 
    ListNode reverse(ListNode a, ListNode b) {
        ListNode pre, cur, next;
        pre = null;
        cur = a;
        next = a;
        while (cur != b) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 23. 合并 K 个升序链表 - 力扣(LeetCode) (opens new window)

class Solution {
    // 优先队列
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length == 0) return null;
        ListNode dummy=new ListNode(-1);
        ListNode p = dummy;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length,(a,b) -> (a.val-b.val));
        for (ListNode head: lists) {
            if(head != null) pq.add(head);
        }
        while (!pq.isEmpty()){
            ListNode node = pq.poll();
            p.next = node;
            if(node.next != null){
                pq.add(node.next);
            }
            p = p.next;
        }
        return dummy.next;
    }
    // 分治合并
    // public ListNode mergeKLists(ListNode[] lists) {
    //     return merge(lists,0,lists.length - 1);
    // }

    // 顺序合并
    // public ListNode mergeKLists(ListNode[] lists) {
    //     ListNode ans = null;
    //     for (int i = 0;i < lists.length; ++i) {
    //         ans = mergeTwoLists(ans, lists[i]);
    //     }
    //     return ans;
    // }

    public ListNode merge(ListNode[] lists, int l,int r) {
        if (l == r) {
            return lists[l];
        }
        if (l > r) {
            return null;
        }
        int mid = (l + r) >> 1;
        return mergeTwoLists(merge(lists,l,mid),merge(lists,mid + 1, r));
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
    ListNode dum = new ListNode(0), cur = dum;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                cur.next = list1;
                list1 = list1.next;
            }
            else {
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        cur.next = list1 != null ? list1 : list2;
        return dum.next;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
编辑 (opens new window)
上次更新: 2025/06/13, 00:51:28
二分查找
双指针(逆向,快慢)

← 二分查找 双指针(逆向,快慢)→

最近更新
01
集成loki
07-04
02
TCP的ESTABLISHED是什么意思
06-24
03
安装1panel
06-24
更多文章>
Theme by Vdoing | Copyright © 2025-2025 plantre | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式