Leetcode "Linked list": Merge Sorted Lists && merge K Sorted Lists

Source: Internet
Author: User

1. Merge Sorted Lists

Topic links

Title Requirements:

Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.

The topic test instructions is to merge two ordered lists into an ordered list. For programming convenience, the dummy node is introduced into the program. The specific procedures are as follows:

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 classSolution {Ten  Public: Onelistnode* mergetwolists (listnode* L1, listnode*L2) { AListNode *Dummy=NewListNode (0); -ListNode *head = nullptr, *start =dummy; -         intFlag =true; the          while(L1 &&L2) -         { -             if(L1->val < l2->val) -             { +Start->next =L1; -L1 = l1->Next; +             } A             Else  at             { -Start->next =L2; -L2 = l2->Next; -             } -Start = start->Next; -         } in          -         if(!L1) toStart->next =L2; +         Else if(!L2) -Start->next =L1; the          *Head = dummy->Next; $         Deletedummy;Panax Notoginsengdummy =nullptr; -          the         returnhead; +     } A};
2. Merge k Sorted Lists

Topic links

Title Requirements:

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

The problem was intended to be a more intuitive approach, but timed out. The specific procedures are as follows:

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 classSolution {Ten  Public: Onelistnode* mergeklists (vector<listnode*>&lists) { A         intSZ =lists.size (); -         if(SZ = =0) -             returnnullptr; the          -ListNode *node =NewListNode (0); -ListNode *head = nullptr, *start =node; -          while(true) +         { -             intCount =0; +             intMinval =Int_max; A             intMinnode =-1; at              for(inti =0; I < sz; i++) -             { -                 if(Lists[i] && Lists[i]->val <minval) -                 { -Minnode =i; -Minval = lists[i]->Val; in                 } -                 Else if(!Lists[i]) tocount++; +             } -              the             if(Count! =sz) *             { $Start->next =Lists[minnode];Panax NotoginsengLists[minnode] = lists[minnode]->Next; -             } the             Else +             { AHead = node->Next; the                 Deletenode; +node =nullptr; -                  $                 returnhead; $             } -              -Start = start->Next; the         } -     }Wuyi};
View Code

Later referring to the online approach, with divide and conquer method will be better, the specific procedures are as follows:

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 classSolution {Ten  Public: OneListNode *mergetwolists (listnode* L1, listnode*L2) { AListNode *node =NewListNode (0); -ListNode *head = nullptr, *start =node; -         intFlag =true; the          while(L1 &&L2) -         { -             if(L1->val < l2->val) -             { +Start->next =L1; -L1 = l1->Next; +             } A             Else  at             { -Start->next =< -L2 = l2->Next; -             } -Start = start->Next; -         } in          -         if(!L1) toStart->next =< +         Else if(!L2) -Start->next =L1; the          *Head = node->Next; $         Deletenode;Panax Notoginsengnode =nullptr; -          the         returnhead; +     } A  theListNode *mergeklists (vector<listnode*>& lists,intLowintHigh ) +     { -         if(Low <High ) $         { $             intMid = (low + high)/2; -             returnMergetwolists (mergeklists (lists, low, mid), mergeklists (lists, Mid +1, High)); -         } the         returnLists[low]; -     }Wuyi      thelistnode* mergeklists (vector<listnode*>&lists) { -         intSZ =lists.size (); Wu         if(SZ = =0) -             returnnullptr; About          $         returnMergeklists (lists,0, Sz-1); -     } -};

Algorithmic analysis excerpted from the same blog post:

Let's analyze the time complexity of the above algorithm. Assuming that there is a total of K lists, the maximum length of each list is n, then the run time satisfies the recursive t (k) = 2T (K/2) +o (n*k). According to the main theorem, the total complexity of the algorithm can be calculated as O (nklogk). If you do not know the main theorem of friends, you can see the main theorem-Wikipedia. Space complexity is the size of the recursive stack O (logk).

Leetcode "Linked list": Merge Sorted Lists && merge K Sorted Lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.