From f18c86b7d8c98c285afea5b43c53b4de05e2fe63 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 13:45:04 +0800 Subject: [PATCH] =?UTF-8?q?Update=2025.=20=E5=90=88=E5=B9=B6=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=8E=92=E5=BA=8F=E7=9A=84=E9=93=BE=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。 --- notes/25. 合并两个排序的链表.md | 44 ++++++++++++++++----------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/notes/25. 合并两个排序的链表.md b/notes/25. 合并两个排序的链表.md index ba0e6330..93472516 100644 --- a/notes/25. 合并两个排序的链表.md +++ b/notes/25. 合并两个排序的链表.md @@ -1,6 +1,6 @@ # 25. 合并两个排序的链表 -[NowCoder](https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&tqId=11169&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/) ## 题目描述 @@ -11,17 +11,17 @@ ### 递归 ```java -public ListNode Merge(ListNode list1, ListNode list2) { - if (list1 == null) - return list2; - if (list2 == null) - return list1; - if (list1.val <= list2.val) { - list1.next = Merge(list1.next, list2); - return list1; +public ListNode trainningPlan(ListNode l1, ListNode l2) { + if (l1 == null) + return l2; + if (l2 == null) + return l1; + if (l1.val <= l2.val) { + l1.next = trainningPlan(l1.next, l2); + return l1; } else { - list2.next = Merge(list1, list2.next); - return list2; + l2.next = trainningPlan(l1, l2.next); + return l2; } } ``` @@ -29,23 +29,23 @@ public ListNode Merge(ListNode list1, ListNode list2) { ### 迭代 ```java -public ListNode Merge(ListNode list1, ListNode list2) { +public ListNode trainningPlan(ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode cur = head; - while (list1 != null && list2 != null) { - if (list1.val <= list2.val) { - cur.next = list1; - list1 = list1.next; + while (l1 != null && l2 != null) { + if (l1.val <= l2.val) { + cur.next = l1; + l1 = l1.next; } else { - cur.next = list2; - list2 = list2.next; + cur.next = l2; + l2 = l2.next; } cur = cur.next; } - if (list1 != null) - cur.next = list1; - if (list2 != null) - cur.next = list2; + if (l1 != null) + cur.next = l1; + if (l2 != null) + cur.next = l2; return head.next; } ```