Leetcode python 012 hard merge K ordered list

Source: Internet
Author: User

#[leetcode] Merge k Sorted Lists merge K ordered list (ascending)

Import NumPy as NP
Import time

Class Node (object):
def __init__ (Self,n,next_node=none):
Self.data=n
Self.next=next_node

Class Linklist (object):
def __init__ (self):
Self.head=none

def init (self,data):
Assert type (data) ==list,type (data)
Self.head=node (Data[0],none)
P=self.head
For i in Data[1:]:
Node=node (i)
P.next=node
P=p.next

Def show (self):
L=[]
P=self.head
While P:
L.append (str (p.data))
P=p.next
Print ('-a '. Join (L))

def hebing (LINK1,LINK2):
P1,p2=link1.head,link2.head
If P1.data>p2.data:
Return hebing (LINK2,LINK1)
While P2:
If not P1.next and P2.next:
P1.next=p2
Return Link1
If P1.data < P2.data and P1.next.data>p2.data:
Px=p1.next
Py=p2.next
P1.next=p2
P2.next=px
P2=py
P1=px
Elif P1.next.data<p2.data:
P1=p1.next
Return Link1

def fenzu (group):
N=len (Group)
If n==1:
return group[0]
Elif n==2:
Return hebing (Group[0],group[1])
Else
Return hebing (Fenzu (GROUP[:N//2]), Fenzu (GROUP[N//2:]))
L1,L2,L3=[1,3,5,9],[0,2,4,6,8,10],[3.1,5.5,15,46]
Link1,link2,link3=linklist (), linklist (), linklist ()
Link1.init (L1)
Link2.init (L2)
Link3.init (L3)
Fenzu ([Link1,link2,link3]). Show ()

Leetcode python 012 hard merge K ordered list

Related Article

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.