-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_k_lists.py
More file actions
56 lines (43 loc) · 1.66 KB
/
Copy pathmerge_k_lists.py
File metadata and controls
56 lines (43 loc) · 1.66 KB
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
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MergeKSortedLists():
from typing import List, Optional
def __init__(self, content = [66,26,46]):
self.content = content
print('>>', '\n', 'CONTENT : ',content)
# Definition for singly-linked list.
def sort_merge(self, total_listnode, listnode):
if total_listnode is None:
return listnode
elif listnode is None:
return total_listnode
if total_listnode.val <= listnode.val:
result = total_listnode
result.next = self.sort_merge(total_listnode.next, listnode)
else:
result = listnode
result.next = self.sort_merge(total_listnode, listnode.next)
return result
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
total_listnode = None
lists_will_append = None
if lists:
for index, item in enumerate(lists):
if item and not total_listnode:
total_listnode = item
lists_will_append = lists[index + 1:len(lists):]
if lists_will_append:
for listnode in lists_will_append:
total_listnode = self.sort_merge(total_listnode, listnode)
return total_listnode
def main(self):
print('\033[92m',self.mergeKLists(self.content), '\033[0m\n........')
# TEST :
if __name__ == '__main__':
text = (
[[1,4,5],[1,3,4],[2,6]] # Need to make linked lists
)
for item in text:
MergeKSortedLists(item).main()