p122 合并 K 个有序链表(leetcode 23)

Posted repinkply

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了p122 合并 K 个有序链表(leetcode 23)相关的知识,希望对你有一定的参考价值。

一:解题思路

方法一:之前做过一道合并2个链表的题目,那么第一种方法就是将数组中的链表两两合并,得到最后的结果。Time:O(k*n),Space:O(1)

方法二:采用分治法,两两合拼。不断递归,最后只剩下一个链表。Time:O(n*log(k)),Space:O(log(k))

二:完整代码示例 (C++版和Java版)

方法一C++:

class Solution 
{
private:
    ListNode* mergeTwoSortedLists(ListNode* l1, ListNode* l2)
    {
        ListNode* dummy = new ListNode(0);
        ListNode* p = dummy;
        while (l1 != NULL && l2 != NULL)
        {
            if (l1->val < l2->val)
            {
                p->next = l1;
                l1 = l1->next;
            }
            else
            {
                p->next = l2;
                l2 = l2->next;
            }

            p = p->next;
        }

        if (l1 != NULL) p->next = l1;
        if (l2 != NULL) p->next = l2;

        return dummy->next;
    }
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) 
    {
        if (lists.size() == 0) return NULL;
        ListNode* result = NULL;
        for (ListNode* list : lists)
        {
            result = mergeTwoSortedLists(result,list);
        }

        return result;
    }
};

方法一Java:

class Solution
    {
        private ListNode mergeTwoSortedLists(ListNode l1,ListNode l2)
        {
              ListNode dummy=new ListNode(0);
              ListNode p=dummy;
              while (l1!=null && l2!=null)
              {
                  if(l1.val<l2.val)
                  {
                      p.next=l1;
                      l1=l1.next;
                  }
                  else
                  {
                      p.next=l2;
                      l2=l2.next;
                  }
                  p=p.next;
              }

              if(l1!=null) p.next=l1;
              if(l2!=null) p.next=l2;

              return dummy.next;
        }

        public ListNode mergeKLists(ListNode[] lists)
        {
               if(lists==null || lists.length==0) return null;
               ListNode result=null;
               for(ListNode list:lists)
               {
                   result=mergeTwoSortedLists(result,list);
               }

               return result;
        }
    }

方法二C++:

class Solution 
{
private:
    ListNode* mergeTwoSortedList(ListNode* l1, ListNode* l2)
    {
            ListNode* dummy = new ListNode(0);
            ListNode* p = dummy;
            while (l1 != NULL && l2 != NULL)
            {
                if (l1->val < l2->val)
                {
                    p->next = l1;
                    l1 = l1->next;
                }
                else
                {
                    p->next = l2;
                    l2 = l2->next;
                }

                p=p->next;
            }

            if (l1 != NULL) p->next = l1;
            if (l2 != NULL) p->next = l2;

            return dummy->next;
    }

    ListNode* merge(vector<ListNode*>& lists, int start, int end)
    {
        if (start == end) return lists[start];
        if (start > end) return NULL;
        int mid = start + (end-start) / 2;
        ListNode* left = merge(lists,start,mid);
        ListNode* right = merge(lists,mid+1,end);

        return mergeTwoSortedList(left,right);
    }
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) 
    {
        if (lists.size() == 0) return NULL;

        return merge(lists,0,lists.size()-1);
    }
};

方法二Java:

class Solution
    {
        private ListNode mergeTwoSortedList(ListNode l1,ListNode l2)
        {
                ListNode dummy=new ListNode(0);
                ListNode p=dummy;
                while (l1!=null && l2!=null)
                {
                    if(l1.val<l2.val)
                    {
                        p.next=l1;
                        l1=l1.next;
                    }
                    else
                    {
                        p.next=l2;
                        l2=l2.next;
                    }
                    p=p.next;
                }
                
                if(l1!=null) p.next=l1;
                if(l2!=null) p.next=l2;
                
                return dummy.next;
        }
        private ListNode merge(ListNode[] lists,int start,int end)
        {
               if(start==end) return lists[start];
               if(start>end) return null;
               int mid=start+(end-start)/2;
               ListNode left=merge(lists,start,mid);
               ListNode right=merge(lists,mid+1,end);
               
               return mergeTwoSortedList(left,right);
        }

        public ListNode mergeKLists(ListNode[] lists)
        {
               if(lists==null || lists.length==0) return null;
               
               return merge(lists,0,lists.length-1);
        }
    }

 

以上是关于p122 合并 K 个有序链表(leetcode 23)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 23 合并 k 个有序链表

leetcode python 012 hard 合并k个有序链表

链表5:6种方法实现合并K个有序链表

LeetCode 23合并K个排序链表

Leetcode merge-k-sorted-lists(合并k路有序链表 最小堆)

LeetCode JavaScript实现 合并链表 题型汇总