Skip to content

Commit 6f25bd4

Browse files
committed
Merge K Lists (#165)
1 parent ddf6018 commit 6f25bd4

5 files changed

Lines changed: 111 additions & 21 deletions

File tree

‎data_structures/list_node.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55

66
class ListNode(Generic[T]):
7-
def __init__(self, val: T):
8-
self.val: T = val
9-
self.next: Optional[ListNode] = None
7+
def __init__(self, val: Optional[T] = None, next_node: Optional['ListNode[T]'] = None):
8+
self.val: Optional[T] = val
9+
self.next: Optional[ListNode] = next_node

‎singly_linked_lists/README.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [Find Middle Node](#find-middle-node)
88
* [Intersection Two Linked Lists](#intersection-two-linked-lists)
99
* [Linked List Cycle](#linked-list-cycle)
10+
* [Merge K Sorted Lists](#merge-k-sorted-lists)
1011
* [Merge Two Sorted Lists](#)
1112
* [Odd Even Linked List](#odd-even-linked-list)
1213
* [Palindrome Linked List](#palindrome-linked-list)
@@ -297,6 +298,39 @@ def has_cycle_with_set(head: Optional['ListNode']) -> bool:
297298
return False
298299
```
299300

301+
## Merge K Sorted Lists
302+
303+
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
304+
305+
Merge all the linked-lists into one sorted linked-list and return it.
306+
307+
```python
308+
def merge_k_lists(lists: Optional[List[ListNode]]) -> Optional[ListNode]:
309+
dummy = ListNode()
310+
current = dummy
311+
312+
lists = [val for val in lists if val]
313+
314+
while lists:
315+
min_node = None
316+
smallest = float('inf')
317+
318+
for index, head in enumerate(lists):
319+
if head and head.val <= smallest:
320+
smallest = head.val
321+
min_node = index
322+
323+
current.next = lists[min_node]
324+
325+
if not lists[min_node].next:
326+
lists.pop(min_node)
327+
else:
328+
lists[min_node] = lists[min_node].next
329+
current = current.next
330+
331+
return dummy.next
332+
```
333+
300334
## Merge Two Sorted Lists
301335

302336
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List, Optional
2+
from data_structures.list_node import ListNode
3+
4+
5+
def merge_k_lists(lists: Optional[List[ListNode]]) -> Optional[ListNode]:
6+
dummy = ListNode()
7+
current = dummy
8+
9+
lists = [val for val in lists if val]
10+
11+
while lists:
12+
min_node = None
13+
smallest = float('inf')
14+
15+
for index, head in enumerate(lists):
16+
if head and head.val <= smallest:
17+
smallest = head.val
18+
min_node = index
19+
20+
current.next = lists[min_node]
21+
22+
if not lists[min_node].next:
23+
lists.pop(min_node)
24+
else:
25+
lists[min_node] = lists[min_node].next
26+
current = current.next
27+
28+
return dummy.next
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from singly_linked_lists.merge_k_lists import merge_k_lists
2+
from data_structures.list_node import ListNode
3+
from test_helpers.test_helpers import get_list_values, get_singly_linked_list
4+
5+
6+
def test_merge_k_lists():
7+
lists = [get_singly_linked_list([1, 2, 3]), get_singly_linked_list([1, 3, 4, 5]), get_singly_linked_list([3, 6])]
8+
assert get_list_values(merge_k_lists(lists)) == [1, 1, 2, 3, 3, 3, 4, 5, 6]
9+
10+
lists = [None, None, get_singly_linked_list([1, 2, 3])]
11+
assert get_list_values(merge_k_lists(lists)) == [1, 2, 3]
12+
13+
lists = []
14+
assert merge_k_lists(lists) is None
15+
16+
lists = [None, None]
17+
assert merge_k_lists(lists) is None

‎test_helpers/test_helpers.py‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ def get_binary_tree_values(root: TreeNode) -> List:
4343
return values
4444

4545

46+
def get_list_values(head: T):
47+
node = head
48+
values = []
49+
while node:
50+
values.append(node.val)
51+
node = node.next
52+
return values
53+
54+
55+
def get_cyclic_list_values(head: ListNode):
56+
if not head:
57+
return []
58+
59+
values = [head.val]
60+
node = head.next
61+
62+
while node is not head:
63+
values.append(node.val)
64+
node = node.next
65+
return values
66+
67+
4668
def get_n_nary_tree():
4769
# 1
4870
# / | \
@@ -87,23 +109,12 @@ def get_n_ary_tree_values(root: Node) -> List:
87109
return values
88110

89111

90-
def get_list_values(head: T):
91-
node = head
92-
values = []
93-
while node:
94-
values.append(node.val)
95-
node = node.next
96-
return values
97-
98-
99-
def get_cyclic_list_values(head: ListNode):
100-
if not head:
101-
return []
112+
def get_singly_linked_list(values: List[T]) -> ListNode[T]:
113+
dummy = ListNode()
114+
current = dummy
102115

103-
values = [head.val]
104-
node = head.next
116+
for value in values:
117+
current.next = ListNode(value)
118+
current = current.next
105119

106-
while node is not head:
107-
values.append(node.val)
108-
node = node.next
109-
return values
120+
return dummy.next

0 commit comments

Comments
 (0)