[LeetCode]Merge k Sorted Lists

题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

思路:我的第一个想法是将lists中的链表两两合并排序,这样时间复杂度是o(n),n为所有链表中的数据,结果Time Limit Exceeded了。。。暂时没有想到太好的其他方法,希望有大神能给点思路,我先抛个砖,以下是我未AC的代码:

 

 1 /**

 2  * Definition for singly-linked list.

 3  * struct ListNode {

 4  *     int val;

 5  *     ListNode *next;

 6  *     ListNode(int x) : val(x), next(NULL) {}

 7  * };

 8  */

 9 class Solution {

10 public:

11     ListNode *mergeKLists(vector<ListNode *> &lists) {

12         if(lists.size() == 0)   return NULL;

13         

14         ListNode *pList=lists[0];

15         

16         vector<ListNode *>::iterator it=lists.begin()+1;

17         for(;it!=lists.end();++it)

18         {

19             pList=MergeLists(pList,*it);

20         }

21         

22         //for(int i=1;i<lists.size();i++)

23         //{

24             //pList=MergeLists(pList,lists[i]);

25         //}

26         

27         return pList;

28     }

29     

30     ListNode *MergeLists(ListNode *list1, ListNode *list2)

31     {

32         ListNode *pList=new ListNode(0);

33         ListNode *pHead=pList;

34         

35         while(list1 && list2)

36         {

37             if(list1->val > list2->val)

38             {

39                 pList->next=list2;

40                 list2=list2->next;

41             }

42             else

43             {

44                 pList->next=list1;

45                 list1=list1->next;

46             }

47             pList=pList->next;

48         }

49         

50         if(!list1)

51         {

52             pList->next=list2;

53         }

54         if(!list2)

55         {

56             pList->next=list1;

57         }

58         

59         pList=pHead;

60         pList=pList->next;

61         pHead->next=NULL;

62         delete(pHead);

63         

64         return pList;

65     }

66 };
View Code

 思路2:今天早上想到用归并排序的思想做一做,时间复杂度可以优化到o(nlogn),感觉很不错啊!于是说写就写,一次AC!好兴奋!

 1 /**

 2  * Definition for singly-linked list.

 3  * struct ListNode {

 4  *     int val;

 5  *     ListNode *next;

 6  *     ListNode(int x) : val(x), next(NULL) {}

 7  * };

 8  */

 9 class Solution {

10 public:

11     ListNode *mergeKLists(vector<ListNode *> &lists) {

12         if(lists.size() == 0)   return NULL;

13         

14         if(lists.size() == 1)   return lists[0];

15         

16         ListNode *pList=mergeKLists(lists, 0, lists.size()-1);

17         

18         return pList;

19     }

20     

21     ListNode *mergeKLists(vector<ListNode *> &lists, int left, int right)

22     {

23         if(left<right)

24         {

25             int mid=left+((right-left)>>1);

26             ListNode *pLeft=mergeKLists(lists,left,mid);

27             ListNode *pRight=mergeKLists(lists,mid+1,right);

28             return MergeLists(pLeft,pRight);

29         }

30         return lists[left];

31     }

32     

33     ListNode *MergeLists(ListNode *list1, ListNode *list2)

34     {

35         ListNode *pList=new ListNode(0);

36         ListNode *pHead=pList;

37         

38         while(list1 && list2)

39         {

40             if(list1->val > list2->val)

41             {

42                 pList->next=list2;

43                 list2=list2->next;

44             }

45             else

46             {

47                 pList->next=list1;

48                 list1=list1->next;

49             }

50             pList=pList->next;

51         }

52         

53         if(!list1)

54         {

55             pList->next=list2;

56         }

57         if(!list2)

58         {

59             pList->next=list1;

60         }

61         

62         pList=pHead;

63         pList=pList->next;

64         pHead->next=NULL;

65         delete(pHead);

66         

67         return pList;

68     }

69 };

 

本博客内容与代码均为作者Jarvis原创,如若转载请注明。

你可能感兴趣的:(LeetCode)