File tree Expand file tree Collapse file tree 7 files changed +268
-112
lines changed
solution/0000-0099/0021.Merge Two Sorted Lists Expand file tree Collapse file tree 7 files changed +268
-112
lines changed Original file line number Diff line number Diff line change 216216
217217> "_ You help the developer community practice for interviews, and there is nothing better we could ask for._ " -- [ Alan Yessenbayev] ( https://opencollective.com/alan-yessenbayev )
218218
219+ ## 推荐者
220+
221+ 知名互联网科技博主 [ @爱可可-爱生活] ( https://weibo.com/fly51fly ) 微博推荐。
222+
223+ [ ![ fly51fly] ( ./images/recommender-fly51fly.png )] ( https://weibo.com/fly51fly )
224+
219225## 许可证
220226
221227<a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a >
Original file line number Diff line number Diff line change @@ -80,6 +80,109 @@ class Solution {
8080}
8181```
8282
83+ ### ** C++**
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* cur = dummy;
101+ while (l1 && l2) {
102+ if (l1->val <= l2->val) {
103+ cur->next = l1;
104+ l1 = l1->next;
105+ } else {
106+ cur->next = l2;
107+ l2 = l2->next;
108+ }
109+ cur = cur->next;
110+ }
111+ if (l1) {
112+ cur->next = l1;
113+ } else if (l2) {
114+ cur->next = l2;
115+ }
116+ return dummy->next;
117+ }
118+ };
119+ ```
120+
121+ ### **JavaScript**
122+
123+ ```js
124+ /**
125+ * Definition for singly-linked list.
126+ * function ListNode(val, next) {
127+ * this.val = (val===undefined ? 0 : val)
128+ * this.next = (next===undefined ? null : next)
129+ * }
130+ */
131+ /**
132+ * @param {ListNode} l1
133+ * @param {ListNode} l2
134+ * @return {ListNode}
135+ */
136+ var mergeTwoLists = function (l1, l2) {
137+ const dummy = new ListNode(0);
138+ let cur = dummy;
139+ while (l1 && l2) {
140+ if (l1.val <= l2.val) {
141+ cur.next = l1;
142+ l1 = l1.next;
143+ } else {
144+ cur.next = l2;
145+ l2 = l2.next;
146+ }
147+ cur = cur.next;
148+ }
149+ cur.next = l1 || l2;
150+ return dummy.next;
151+ };
152+ ```
153+
154+ ### ** Go**
155+
156+ ``` go
157+ /* *
158+ * Definition for singly-linked list.
159+ * type ListNode struct {
160+ * Val int
161+ * Next *ListNode
162+ * }
163+ */
164+ func mergeTwoLists (l1 *ListNode , l2 *ListNode ) *ListNode {
165+ dummy := &ListNode{}
166+ cur := dummy
167+ for l1 != nil && l2 != nil {
168+ if l1.Val <= l2.Val {
169+ cur.Next = l1
170+ l1 = l1.Next
171+ } else {
172+ cur.Next = l2
173+ l2 = l2.Next
174+ }
175+ cur = cur.Next
176+ }
177+ if l1 != nil {
178+ cur.Next = l1
179+ } else if l2 != nil {
180+ cur.Next = l2
181+ }
182+ return dummy.Next
183+ }
184+ ```
185+
83186### ** ...**
84187
85188```
Original file line number Diff line number Diff line change @@ -79,6 +79,109 @@ class Solution {
7979}
8080```
8181
82+ ### ** C++**
83+
84+ ``` cpp
85+ /* *
86+ * Definition for singly-linked list.
87+ * struct ListNode {
88+ * int val;
89+ * ListNode *next;
90+ * ListNode() : val(0), next(nullptr) {}
91+ * ListNode(int x) : val(x), next(nullptr) {}
92+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
93+ * };
94+ */
95+ class Solution {
96+ public:
97+ ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
98+ ListNode* dummy = new ListNode(0);
99+ ListNode* cur = dummy;
100+ while (l1 && l2) {
101+ if (l1->val <= l2->val) {
102+ cur->next = l1;
103+ l1 = l1->next;
104+ } else {
105+ cur->next = l2;
106+ l2 = l2->next;
107+ }
108+ cur = cur->next;
109+ }
110+ if (l1) {
111+ cur->next = l1;
112+ } else if (l2) {
113+ cur->next = l2;
114+ }
115+ return dummy->next;
116+ }
117+ };
118+ ```
119+
120+ ### **JavaScript**
121+
122+ ```js
123+ /**
124+ * Definition for singly-linked list.
125+ * function ListNode(val, next) {
126+ * this.val = (val===undefined ? 0 : val)
127+ * this.next = (next===undefined ? null : next)
128+ * }
129+ */
130+ /**
131+ * @param {ListNode} l1
132+ * @param {ListNode} l2
133+ * @return {ListNode}
134+ */
135+ var mergeTwoLists = function (l1, l2) {
136+ const dummy = new ListNode(0);
137+ let cur = dummy;
138+ while (l1 && l2) {
139+ if (l1.val <= l2.val) {
140+ cur.next = l1;
141+ l1 = l1.next;
142+ } else {
143+ cur.next = l2;
144+ l2 = l2.next;
145+ }
146+ cur = cur.next;
147+ }
148+ cur.next = l1 || l2;
149+ return dummy.next;
150+ };
151+ ```
152+
153+ ### ** Go**
154+
155+ ``` go
156+ /* *
157+ * Definition for singly-linked list.
158+ * type ListNode struct {
159+ * Val int
160+ * Next *ListNode
161+ * }
162+ */
163+ func mergeTwoLists (l1 *ListNode , l2 *ListNode ) *ListNode {
164+ dummy := &ListNode{}
165+ cur := dummy
166+ for l1 != nil && l2 != nil {
167+ if l1.Val <= l2.Val {
168+ cur.Next = l1
169+ l1 = l1.Next
170+ } else {
171+ cur.Next = l2
172+ l2 = l2.Next
173+ }
174+ cur = cur.Next
175+ }
176+ if l1 != nil {
177+ cur.Next = l1
178+ } else if l2 != nil {
179+ cur.Next = l2
180+ }
181+ return dummy.Next
182+ }
183+ ```
184+
82185### ** ...**
83186
84187```
Original file line number Diff line number Diff line change 33 * struct ListNode {
44 * int val;
55 * ListNode *next;
6- * ListNode(int x) : val(x), next(NULL) {}
6+ * ListNode() : val(0), next(nullptr) {}
7+ * ListNode(int x) : val(x), next(nullptr) {}
8+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
79 * };
810 */
911class Solution {
1012public:
1113 ListNode* mergeTwoLists (ListNode* l1, ListNode* l2) {
12- ListNode *a1=new ListNode (0 );
13- ListNode *head=a1;
14- while (l1!=NULL &&l2!=NULL )
15- {
16- if (l1->val <l2->val )
17- {
18- a1->next =l1;
19- l1=l1->next ;
20- a1=a1->next ;
21- }
22- else
23- {
24- a1->next =l2;
25- l2=l2->next ;
26- a1=a1->next ;
14+ ListNode* dummy = new ListNode (0 );
15+ ListNode* cur = dummy;
16+ while (l1 && l2) {
17+ if (l1->val <= l2->val ) {
18+ cur->next = l1;
19+ l1 = l1->next ;
20+ } else {
21+ cur->next = l2;
22+ l2 = l2->next ;
2723 }
24+ cur = cur->next ;
25+ }
26+ if (l1) {
27+ cur->next = l1;
28+ } else if (l2) {
29+ cur->next = l2;
2830 }
29- if (l1==NULL )
30- a1->next =l2;
31- if (l2==NULL )
32- a1->next =l1;
33- return head->next ;
34-
35-
31+ return dummy->next ;
3632 }
37- };
33+ };
Original file line number Diff line number Diff line change 66 * }
77 */
88 func mergeTwoLists (l1 * ListNode , l2 * ListNode ) * ListNode {
9- if l1 == nil {
10- return l2
11- }
12-
13- if l2 == nil {
14- return l1
15- }
16-
17- var p * ListNode
18-
19- if l1 .Val > l2 .Val {
20- p = l2
21- l2 = l2 .Next
22- }else {
23- p = l1
24- l1 = l1 .Next
25- }
26- var head * ListNode = p
27- p .Next = nil
28-
9+ dummy := & ListNode {}
10+ cur := dummy
2911 for l1 != nil && l2 != nil {
30- if l1 .Val > l2 .Val {
31- p .Next = l2
32- l2 = l2 .Next
33- }else {
34- p .Next = l1
12+ if l1 .Val <= l2 .Val {
13+ cur .Next = l1
3514 l1 = l1 .Next
15+ } else {
16+ cur .Next = l2
17+ l2 = l2 .Next
3618 }
37- p = p .Next
38- p .Next = nil
39- }
40-
41- if l1 != nil {
42- p .Next = l1
19+ cur = cur .Next
4320 }
44- if l2 != nil {
45- p .Next = l2
21+ if l1 != nil {
22+ cur .Next = l1
23+ } else if l2 != nil {
24+ cur .Next = l2
4625 }
47-
48- return head
26+ return dummy .Next
4927}
You can’t perform that action at this time.
0 commit comments