Skip to content

Commit 4e23c3d

Browse files
committed
add cpp: 21.merge-two-sorted-lists
1 parent 214c931 commit 4e23c3d

File tree

5 files changed

+218
-117
lines changed

5 files changed

+218
-117
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109

110110
- [x] [2.两数相加](./problems/2.add-two-numbers.md)
111111
- [x] [19.删除链表的倒数第 N 个结点](./problems/0019.remove-nth-node-from-end-of-list.md)
112-
- [x] [21.合并两个有序链表](./basic/linked-list/ext-merge-two-sorted-lists.md)
112+
- [x] [21.合并两个有序链表](./problems/0021.merge-two-sorted-lists.md)
113113
- [x] [83.删除排序链表中的重复元素](./basic/linked-list/ext-remove-duplicates-from-sorted-list.md)
114114
- [x] [206.反转链表](./basic/linked-list/ext-reverse-linked-list.md)
115115

basic/linked-list/ext-merge-two-sorted-lists.md

Lines changed: 0 additions & 116 deletions
This file was deleted.

problems/0019.remove-nth-node-from-end-of-list.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
44

5+
- [19. 删除链表的倒数第 N 个结点](#19-删除链表的倒数第-n-个结点)
6+
- [题目描述](#题目描述)
7+
- [方法1: 快慢指针](#方法1-快慢指针)
8+
- [思路](#思路)
9+
- [复杂度分析](#复杂度分析)
10+
- [代码](#代码)
11+
512
## 题目描述
613

714
```
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# 21. 合并两个有序链表
2+
3+
https://leetcode-cn.com/problems/merge-two-sorted-lists/
4+
5+
- [21. 合并两个有序链表](#21-合并两个有序链表)
6+
- [题目描述](#题目描述)
7+
- [方法 1: 迭代](#方法-1-迭代)
8+
- [思路](#思路)
9+
- [复杂度分析](#复杂度分析)
10+
- [代码(JavaScript/C++)](#代码javascriptc)
11+
- [方法 2: 递归](#方法-2-递归)
12+
- [思路](#思路-1)
13+
- [复杂度分析](#复杂度分析-1)
14+
- [代码(JavaScript/C++)](#代码javascriptc-1)
15+
16+
## 题目描述
17+
18+
```
19+
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
20+
21+
示例:
22+
23+
输入:1->2->4, 1->3->4
24+
输出:1->1->2->3->4->4
25+
26+
来源:力扣(LeetCode)
27+
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
28+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
29+
```
30+
31+
## 方法 1: 迭代
32+
33+
### 思路
34+
35+
- 用两个指针分别遍历两个链表,比较两个指针节点的大小。
36+
- 将较小的那个节点加到新链表中,然后指针后移。
37+
- 较大的那个节点指针不动,下一轮继续比较。
38+
- 如果两个链表长度不一样,继续遍历将多出来的节点加到新链表中就好了。
39+
40+
### 复杂度分析
41+
42+
- 时间复杂度:$O(N)$, N 为新链表长度。
43+
- 空间复杂度:$O(1)$。
44+
45+
### 代码(JavaScript/C++)
46+
47+
JavaScript Code
48+
49+
```js
50+
/**
51+
* Definition for singly-linked list.
52+
* function ListNode(val, next) {
53+
* this.val = (val===undefined ? 0 : val)
54+
* this.next = (next===undefined ? null : next)
55+
* }
56+
*/
57+
/**
58+
* @param {ListNode} l1
59+
* @param {ListNode} l2
60+
* @return {ListNode}
61+
*/
62+
var mergeTwoLists = function (l1, l2) {
63+
const dummy = new ListNode();
64+
let p1 = l1,
65+
p2 = l2,
66+
cur = dummy;
67+
68+
while (p1 || p2) {
69+
if (!p2 || (p1 && p1.val <= p2.val)) {
70+
cur.next = new ListNode(p1.val);
71+
p1 = p1.next;
72+
} else {
73+
cur.next = new ListNode(p2.val);
74+
p2 = p2.next;
75+
}
76+
cur = cur.next;
77+
}
78+
79+
return dummy.next;
80+
};
81+
```
82+
83+
C++ Code
84+
85+
```cpp
86+
/**
87+
* Definition for singly-linked list.
88+
* struct ListNode {
89+
* int val;
90+
* ListNode *next;
91+
* ListNode() : val(0), next(nullptr) {}
92+
* ListNode(int x) : val(x), next(nullptr) {}
93+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
94+
* };
95+
*/
96+
class Solution {
97+
public:
98+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
99+
ListNode *dummy = new ListNode(0);
100+
ListNode *tail = dummy;
101+
102+
while (l1 && l2) {
103+
if (l1->val <= l2->val) {
104+
tail->next = new ListNode(l1->val);
105+
l1 = l1->next;
106+
} else {
107+
tail->next = new ListNode(l2->val);
108+
l2 = l2->next;
109+
}
110+
tail = tail->next;
111+
}
112+
while (l1) {
113+
tail->next = new ListNode(l1->val);
114+
l1 = l1->next;
115+
tail = tail->next;
116+
}
117+
while (l2) {
118+
tail->next = new ListNode(l2->val);
119+
l2 = l2->next;
120+
tail = tail->next;
121+
}
122+
return dummy->next;
123+
}
124+
};
125+
```
126+
127+
## 方法 2: 递归
128+
129+
### 思路
130+
131+
- 如果 `l1.val < l2.val`,那就把 l1 作为合并链表的头部返回,然后继续合并 `l1.next``l2` 之后的节点。
132+
- 如果 `l1.val > l2.val`,那就把 l2 作为合并链表的头部返回,然后继续合并 `l2.next``l1` 之后的节点。
133+
- 递归出口:
134+
- 没有节点了。
135+
- 只剩一个节点,直接返回。
136+
137+
### 复杂度分析
138+
139+
- 时间复杂度:$O(N)$, N 为新链表长度。
140+
- 空间复杂度:$O(N)$, N 为新链表长度,递归栈的空间。
141+
142+
### 代码(JavaScript/C++)
143+
144+
JavaScript Code
145+
146+
```js
147+
/**
148+
* Definition for singly-linked list.
149+
* function ListNode(val, next) {
150+
* this.val = (val===undefined ? 0 : val)
151+
* this.next = (next===undefined ? null : next)
152+
* }
153+
*/
154+
/**
155+
* @param {ListNode} l1
156+
* @param {ListNode} l2
157+
* @return {ListNode}
158+
*/
159+
var mergeTwoLists = function (l1, l2) {
160+
if (!l1) return l2;
161+
if (!l2) return l1;
162+
163+
if (l1.val < l2.val) {
164+
l1.next = mergeTwoLists(l1.next, l2);
165+
return l1;
166+
} else {
167+
l2.next = mergeTwoLists(l1, l2.next);
168+
return l2;
169+
}
170+
};
171+
```
172+
173+
C++ Code
174+
175+
```cpp
176+
/**
177+
* Definition for singly-linked list.
178+
* struct ListNode {
179+
* int val;
180+
* ListNode *next;
181+
* ListNode() : val(0), next(nullptr) {}
182+
* ListNode(int x) : val(x), next(nullptr) {}
183+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
184+
* };
185+
*/
186+
class Solution {
187+
public:
188+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
189+
if (!l1) return l2;
190+
if (!l2) return l1;
191+
192+
if (l1->val < l2->val) {
193+
l1->next = mergeTwoLists(l1->next, l2);
194+
return l1;
195+
} else {
196+
l2->next = mergeTwoLists(l1, l2->next);
197+
return l2;
198+
}
199+
}
200+
};
201+
```
202+
203+
更多题解可以访问:[https://github.com/suukii/91-days-algorithm](https://github.com/suukii/91-days-algorithm)

problems/2.add-two-numbers.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
https://leetcode-cn.com/problems/add-two-numbers/
44

5+
- [2. 两数相加](#2-两数相加)
6+
- [题目描述](#题目描述)
7+
- [方法1: 模拟](#方法1-模拟)
8+
- [思路](#思路)
9+
- [复杂度分析](#复杂度分析)
10+
- [代码](#代码)
11+
512
## 题目描述
613

714
```

0 commit comments

Comments
 (0)