Skip to content

Commit 21f51a9

Browse files
authored
Merge pull request #5 from colorbox/21
21. Merge Two Sorted Lists
2 parents db1163b + 8b74b34 commit 21f51a9

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

21/1.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
if(list1 == nullptr)return list2;
15+
if(list2 == nullptr)return list1;
16+
17+
ListNode *ret;
18+
if(list1->val < list2->val){
19+
ret = list1;
20+
list1 = list1->next;
21+
} else{
22+
ret = list2;
23+
list2 = list2->next;
24+
}
25+
ListNode *current = ret;
26+
27+
while(list1 != nullptr || list2 != nullptr){
28+
if(list1 == nullptr){
29+
current->next = list2;
30+
return ret;
31+
}
32+
33+
if(list2 == nullptr){
34+
current->next = list1;
35+
return ret;
36+
}
37+
38+
if(list1->val < list2->val){
39+
current->next = list1;
40+
list1 = list1->next;
41+
}else{
42+
current->next = list2;
43+
list2 = list2->next;
44+
}
45+
current = current->next;
46+
}
47+
48+
return ret;
49+
}
50+
};

21/2.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
if(list1 == nullptr)return list2;
15+
if(list2 == nullptr)return list1;
16+
17+
// always list1 <= list2
18+
if(list1->val > list2->val){
19+
swap(list1, list2);
20+
}
21+
22+
ListNode* ret = list1;
23+
list1 = list1->next;
24+
25+
ListNode *current = ret;
26+
27+
while(list1 != nullptr && list2 != nullptr){
28+
// always list1 <= list2
29+
if(list1->val > list2->val){
30+
swap(list1, list2);
31+
}
32+
33+
current->next = list1;
34+
list1 = list1->next;
35+
36+
current = current->next;
37+
}
38+
39+
if(list1 == nullptr){
40+
current->next = list2;
41+
return ret;
42+
}
43+
44+
if(list2 == nullptr){
45+
current->next = list1;
46+
return ret;
47+
}
48+
49+
return ret;
50+
}
51+
};

21/3.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
ListNode ret = ListNode();
15+
ListNode *current = &ret;
16+
17+
while(list1 != nullptr && list2 != nullptr){
18+
if(list1->val > list2->val){
19+
swap(list1, list2);
20+
}
21+
22+
current->next = list1;
23+
current = current->next;
24+
list1 = list1->next;
25+
}
26+
27+
if(list1 == nullptr){
28+
current->next = list2;
29+
}
30+
if(list2 == nullptr){
31+
current->next = list1;
32+
}
33+
34+
return ret.next;
35+
}
36+
};

21/4.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
ListNode root = ListNode();
15+
ListNode *current = &root;
16+
17+
ListNode *smaller = list1;
18+
ListNode *bigger = list2;
19+
20+
while (smaller && bigger) {
21+
if (smaller->val > bigger->val) {
22+
swap(smaller, bigger);
23+
}
24+
25+
current->next = smaller;
26+
current = current->next;
27+
smaller = smaller->next;
28+
}
29+
30+
if (smaller) {
31+
current->next = smaller;
32+
}
33+
if (bigger) {
34+
current->next = bigger;
35+
}
36+
37+
return root.next;
38+
}
39+
};

0 commit comments

Comments
 (0)