# LeetCode - 23. 合并K个排序链表

### 23. 合并K个排序链表

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *res = NULL, *cur = NULL;
int val;
} else {
}
if (!res) {
res = new ListNode(val);
cur = res;
} else {
cur->next = new ListNode(val);
cur = cur->next;
}
}
cur = cur->next;
}
cur = cur->next;
}
return res;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty()) return NULL;
int n = lists.size();
while (n > 1) {
int m = (n + 1) / 2;
for (int k = 0; k < n / 2; ++k) {
lists[k] = mergeTwoLists(lists[k], lists[k + m]);
}
n = m;
}
return lists[0];
}
};


/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *dummy = new ListNode(-1), *cur = dummy;
} else {
}
}
ListNode *res = dummy->next;
delete dummy;
return res;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty()) return NULL;
int n = lists.size();
while (n > 1) {
int m = (n + 1) / 2;
for (int k = 0; k < n / 2; ++k) {
lists[k] = mergeTwoLists(lists[k], lists[k + m]);
}
n = m;
}
return lists[0];
}
};


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客