码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-04-13 22:48:50      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:space   next   tor   public   else   tno   leetcode   length   node   

一:解题思路

方法一:之前做过一道合并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)

标签:space   next   tor   public   else   tno   leetcode   length   node   

原文地址:https://www.cnblogs.com/repinkply/p/12694187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!