-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMerge-K-Sorted-Lists.py
48 lines (45 loc) · 1.16 KB
/
Merge-K-Sorted-Lists.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
#! /usr/bin/env python
#! -*- coding=utf-8 -*-
# Date: 2019-11-14
# Author: Bryce
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
######## Python Solution 1:
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
res =[]
dummyHead = ListNode(0)
temp = dummyHead
k = len(lists)
if k==0:
return None
if k==1:
return lists[0]
l1 = lists[0]
l2 = lists[1]
for i in range (1,k):
l2 = lists[i]
while l1 and l2:
if l1.val < l2.val:
temp.next = l1
l1 = l1.next
else:
temp.next = l2
l2 = l2.next
temp = temp.next
if l1:
temp.next = l1
else:
temp.next = l2
l1 = dummyHead.next
temp = dummyHead
res = dummyHead.next
return res
######## Python Solution 2:(分治法)