leetcode(리트코드)21-Merge Two Sorted Lists
leetcode(리트코드)21-Merge Two Sorted Lists
leetcode 21 - Merge Two Sorted Lists 문제입니다.
1. 문제
https://leetcode.com/problems/merge-two-sorted-lists/
2. Input , Output
Constraints:
- The number of nodes in both lists is in the range [0, 50].
- -100 <= Node.val <= 100
- Both l1 and l2 are sorted in non-decreasing order.
3. 분류 및 난이도
Eazy 난이도 문제입니다.
leetcode Top 100 Liked의 문제입니다.
4. 문제 해석
- 두 리스트를 합병정렬 합니다.
5. code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* result = new ListNode();
ListNode* head = result;
while(l1!=nullptr && l2!=nullptr)
{
if(l1->val >= l2->val)
{
result->next = l2;
l2=l2->next;
}
else
{
result->next=l1;
l1=l1->next;
}
result= result->next;
}
while(l1!=nullptr)
{
result->next=l1;
result=result->next;
l1=l1->next;
}
while(l2!=nullptr)
{
result->next=l2;
result=result->next;
l2=l2->next;
}
return head->next;
}
};
6. 결과 및 후기, 개선점
시간(81%)
시간0ms(100%) 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
//임시 노드
ListNode dummy(-3);
//그것을 가리키는 꼬리노드
ListNode *tail = &dummy;
//l1과 l2가 nullptr이 아니면
while (l1 and l2) {
if (l1->val < l2->val) {
tail->next = l1;
l1 = l1->next;
} else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
//끝난 것을 기준으로 다른 노드를 아예 갖다 붙임.
tail->next = l1 ? l1 : l2;
return dummy.next;
}
};
기존 내 코드에서 개선한 코드 0ms 100%
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* result = new ListNode();
ListNode* head = result;
while(l1!=nullptr && l2!=nullptr)
{
if(l1->val >= l2->val)
{
result->next = l2;
l2=l2->next;
}
else
{
result->next=l1;
l1=l1->next;
}
result= result->next;
}
//이 부분
result->next = l1 ? l1 : l2;
return head->next;
}
};
This post is licensed under CC BY 4.0 by the author.