-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeKLists.py
56 lines (53 loc) · 1.58 KB
/
mergeKLists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode[]} lists
# @return {ListNode}
def mergeKLists(self, lists):
n = len(lists)
if n == 0 :
return None
if n == 1:
return lists[0]
mid = n/2
l = self.mergeKLists(lists[:mid])
r = self.mergeKLists(lists[mid:])
return self.mergeTwoLists(l,r)
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def mergeTwoLists(self, l1, l2):
prehead = ListNode(0)
cur = prehead
while(l1 and l2):
if l1.val < l2.val:
cur.next = l1
while(l1.next and l1.next.val == l1.val):
l1 = l1.next
cur = l1
l1 = l1.next
elif l1.val > l2.val:
cur.next = l2
while(l2.next and l2.next.val == l2.val):
l2 = l2.next
cur = l2
l2 = l2.next
else:
cur.next = l1
while(l1.next and l1.next.val == l1.val):
l1 = l1.next
cur = l1
l1 = l1.next
cur.next = l2
while(l2.next and l2.next.val == l2.val):
l2 = l2.next
cur = l2
l2 = l2.next
if l1:
cur.next = l1
elif l2:
cur.next = l2
return prehead.next