【leetcode 刷题日记】12-合并K个有序链表(C++)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        vector<int> nums;
        for(auto it = lists.begin();it != lists.end();it++)
        {
            ListNode* node = *it;
            while(node)
            {    
                nums.push_back(node->val);
                node = node->next;
            }
        }
        ListNode* ihead = new ListNode(0);
        ListNode* node = ihead;
        sort(nums.begin(),nums.end());
        for(auto it = nums.begin();it != nums.end();it++)
        {
            node->next = new ListNode(*it);
            node = node->next;
        }
        return ihead->next;
    }
};

今天太忙啦,先这样打卡,明天补上!

发布了34 篇原创文章 · 获赞 24 · 访问量 2013

猜你喜欢

转载自blog.csdn.net/fengshiyu1997/article/details/104829695