-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMergeKSortedLists.py
More file actions
30 lines (28 loc) · 802 Bytes
/
Copy pathMergeKSortedLists.py
File metadata and controls
30 lines (28 loc) · 802 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1
if l1.val<l2.val:
l1.next=self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next=self.mergeTwoLists(l1,l2.next)
return l2
# @param a list of ListNode
# @return a ListNode
def mergeKLists(self, lists):
if len(lists)==0:
return None
if len(lists)==1:
return lists[0]
head = lists[0]
for x in xrange(1,len(lists)):
head=self.mergeTwoLists(head,lists[x])
return head