Skip to content

Commit 6aae983

Browse files
maldz3github-actionscclauss
authored
Create merge_two_lists.py that implements merging of two sorted linked lists (TheAlgorithms#3874)
* Create merge_two_lists.py that implements merging of two sorted linked lists * Update merge_two_lists.py Fixed formatting errors * Fixed trailing whitespace * Change name of function to def __str__() * updating DIRECTORY.md * Imported classes from singly_linked_list.py * Update merge_two_lists.py * Update merge_two_lists.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 9aa5a02 commit 6aae983

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
* [From Sequence](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/from_sequence.py)
154154
* [Has Loop](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/has_loop.py)
155155
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/is_palindrome.py)
156+
* [Merge Two Lists](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/merge_two_lists.py)
156157
* [Middle Element Of Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/middle_element_of_linked_list.py)
157158
* [Print Reverse](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/print_reverse.py)
158159
* [Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/singly_linked_list.py)

Diff for: data_structures/linked_list/merge_two_lists.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Algorithm that merges two sorted linked lists into one sorted linked list.
3+
"""
4+
from __future__ import annotations
5+
6+
from collections.abc import Iterable, Iterator
7+
from dataclasses import dataclass
8+
from typing import Optional
9+
10+
test_data_odd = (3, 9, -11, 0, 7, 5, 1, -1)
11+
test_data_even = (4, 6, 2, 0, 8, 10, 3, -2)
12+
13+
14+
@dataclass
15+
class Node:
16+
data: int
17+
next: Optional[Node]
18+
19+
20+
class SortedLinkedList:
21+
def __init__(self, ints: Iterable[int]) -> None:
22+
self.head: Optional[Node] = None
23+
for i in reversed(sorted(ints)):
24+
self.head = Node(i, self.head)
25+
26+
def __iter__(self) -> Iterator[int]:
27+
"""
28+
>>> tuple(SortedLinkedList(test_data_odd)) == tuple(sorted(test_data_odd))
29+
True
30+
>>> tuple(SortedLinkedList(test_data_even)) == tuple(sorted(test_data_even))
31+
True
32+
"""
33+
node = self.head
34+
while node:
35+
yield node.data
36+
node = node.next
37+
38+
def __len__(self) -> int:
39+
"""
40+
>>> for i in range(3):
41+
... len(SortedLinkedList(range(i))) == i
42+
True
43+
True
44+
True
45+
>>> len(SortedLinkedList(test_data_odd))
46+
8
47+
"""
48+
return len(tuple(iter(self)))
49+
50+
def __str__(self) -> str:
51+
"""
52+
>>> str(SortedLinkedList([]))
53+
''
54+
>>> str(SortedLinkedList(test_data_odd))
55+
'-11 -> -1 -> 0 -> 1 -> 3 -> 5 -> 7 -> 9'
56+
>>> str(SortedLinkedList(test_data_even))
57+
'-2 -> 0 -> 2 -> 3 -> 4 -> 6 -> 8 -> 10'
58+
"""
59+
return " -> ".join([str(node) for node in self])
60+
61+
62+
def merge_lists(
63+
sll_one: SortedLinkedList, sll_two: SortedLinkedList
64+
) -> SortedLinkedList:
65+
"""
66+
>>> SSL = SortedLinkedList
67+
>>> merged = merge_lists(SSL(test_data_odd), SSL(test_data_even))
68+
>>> len(merged)
69+
16
70+
>>> str(merged)
71+
'-11 -> -2 -> -1 -> 0 -> 0 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10'
72+
>>> list(merged) == list(sorted(test_data_odd + test_data_even))
73+
True
74+
"""
75+
return SortedLinkedList(list(sll_one) + list(sll_two))
76+
77+
78+
if __name__ == "__main__":
79+
import doctest
80+
81+
doctest.testmod()
82+
SSL = SortedLinkedList
83+
print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))

0 commit comments

Comments
 (0)