-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_23.cpp
More file actions
57 lines (50 loc) · 1.63 KB
/
Copy pathleetcode_23.cpp
File metadata and controls
57 lines (50 loc) · 1.63 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 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* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty()) return nullptr; // 빈 리스트 처리
// Divide and Conquer 방식으로 리스트 병합
while (lists.size() > 1) {
vector<ListNode*> mergedLists;
for (int i = 0; i < lists.size(); i += 2) {
ListNode* list1 = lists[i];
ListNode* list2 = (i + 1 < lists.size()) ? lists[i + 1] : nullptr;
mergedLists.push_back(merge(list1, list2));
}
lists = mergedLists;
}
return lists.front(); // 최종 병합된 리스트 반환
}
private:
ListNode* merge(ListNode* &list1, ListNode* &list2){
ListNode* dummy = new ListNode(0); // 더미 노드 생성
ListNode* current = dummy;
while(list1 != nullptr && list2 != nullptr){
if(list1->val < list2->val){
current->next = list1;
list1 = list1->next;
} else {
current->next = list2;
list2 = list2->next;
}
current = current->next;
}
if(list1 != nullptr) {
current->next = list1;
} else {
current->next = list2;
}
ListNode* res = dummy->next;
delete dummy; // 메모리 해제
return res;
}
};