. Merge k Sorted Lists

Source: Internet
Author: User

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

AC Code:

defMergeklists_merge_sort (self, lists):defmerge (Lst1, lst2): Dummy= PT = ListNode (-1)             whileLst1 andLst2:ifLst1.val <Lst2.val:pt.next=Lst1 Lst1=Lst1.nextElse: Pt.next=Lst2 Lst2=Lst2.next PT=Pt.next Pt.next= Lst1if  notLst2ElseLst2returnDummy.nextif  notlists:returnNoneifLen (lists) = = 1:            returnLists[0] Mid= Len (lists)/2 Left=Self.mergeklists_merge_sort (Lists[:mid]) right=Self.mergeklists_merge_sort (lists[mid:])returnMerge (left, right)

Start with recursion and find out the timeout. Later borrowed from the online code, using the merge sort.

In addition, there are methods such as priority queuing, which are supplemented later.

. 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.