Skip to content

Commit 876e916

Browse files
committed
feat: 合并两个有序列表,用链表+递归实现
1 parent 6095bd6 commit 876e916

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

algorithms/longestCommonPrefix-jcc/index.html

-13
This file was deleted.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var mergeTwoLists = function(list1, list2) {
2+
// 数组的解法
3+
// let targetList = list1;
4+
// targetList.push(...list2);
5+
6+
// if (targetList.length === 0 || targetList === 1) return targetList;
7+
8+
// targetList.sort((a, b) => a - b);
9+
// return targetList;
10+
11+
// 链表
12+
if (list1 === null) return list2;
13+
if (list2 === null) return list1;
14+
15+
if (list1.val > list2.val) {
16+
list1.next = mergeTwoLists(list1.next, list2);
17+
return list1;
18+
} else {
19+
list2.next = mergeTwoLists(list1, list2.next);
20+
return list2
21+
}
22+
};
23+
24+
console.log(mergeTwoLists([1, 2, 3], [2, 3, 8]));

0 commit comments

Comments
 (0)