leetcode 23 合并k个升序列表
LeetCode 合并 23
2023-09-27 14:29:24 时间
合并k个升序列表
高频题(数组转换)
/**
* 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) {
ListNode* head = new ListNode;
vector<int> nums;
for(int i=0 ; i<lists.size() ;i++)
{
ListNode* tmp = lists[i];
while(tmp!=nullptr)
{
nums.push_back(tmp->val);
tmp = tmp->next;
}
}
sort(nums.begin(),nums.end());
ListNode* tmp = head;
for(int i=0 ; i<nums.size();i++)
{
ListNode* node = new ListNode(nums[i]);
tmp->next = node;
tmp = tmp->next;
}
return head->next;
}
};
相关文章
- Leetcode: Guess Number Higher or Lower
- Leetcode: Spiral Matrix II
- 【Leetcode】21. 合并两个有序链表(简单)
- JS leetcode 合并两个有序数组 解题分析
- [LeetCode] Linked List Cycle II
- [LeetCode] 3Sum
- [LeetCode]1431. 拥有最多糖果的孩子
- 101、【树与二叉树】leetcode ——617. 合并二叉树:递归法+迭代法(C++版本)
- 【leetcode周赛记录】第80场双周赛记录
- 【LeetCode】226. Invert Binary Tree
- 【Leetcode 】21:合并两个有序链表(Python)
- [LeetCode] 801. Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换
- [LeetCode] Contain Virus 包含病毒
- [LeetCode] Delete and Earn 删除与赚取
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
- leetcode算法88.合并两个有序数组
- leetcode算法21.合并两个有序链表