Update 25. 合并两个排序的链表.md

题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
CyC2018
2026-10-28 13:45:04 +08:00
parent f5fae6e5c2
commit f18c86b7d8
+22 -22
View File
@@ -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;
}
```