Skip to content

Commit 8b74b34

Browse files
committed
レビュー指摘を適用したコードを追加
1 parent f6be63f commit 8b74b34

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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)