-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create merge_two_lists.py that implements merging of two sorted linked lists #3874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e9a2ff
Create merge_two_lists.py that implements merging of two sorted linke…
maldz3 2836d72
Update merge_two_lists.py
maldz3 febed14
Fixed trailing whitespace
maldz3 f335a5c
Change name of function to def __str__()
maldz3 ffb2e6b
updating DIRECTORY.md
697665a
Imported classes from singly_linked_list.py
maldz3 295ec36
Update merge_two_lists.py
cclauss 3441cdf
Merge branch 'master' into maliha_merge2lists
cclauss 37de983
Update merge_two_lists.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
""" | ||
Algorithm that merges two sorted linked lists into one sorted linked list. | ||
""" | ||
|
||
|
||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.next = None | ||
|
||
|
||
def create_linked_list(num_list: list) -> Node: | ||
""" | ||
>>> node = create_linked_list([3,2,1]) | ||
>>> node.data | ||
1 | ||
>>> node = create_linked_list([9,6,-3,5,8]) | ||
>>> node.data | ||
-3 | ||
>>> create_linked_list([]) | ||
|
||
""" | ||
if not num_list: | ||
return None | ||
|
||
num_list.sort() | ||
current = head = Node(num_list[0]) | ||
for i in range(1, len(num_list)): | ||
current.next = Node(num_list[i]) | ||
current = current.next | ||
return head | ||
|
||
|
||
def str_linked_list(head: Node) -> str: | ||
""" | ||
>>> n1 = Node(1) | ||
>>> n2 = Node(2) | ||
>>> n3 = Node(3) | ||
>>> n1.next = n2 | ||
>>> n2.next = n3 | ||
>>> str_linked_list(n1) | ||
'1->2->3' | ||
""" | ||
int_list = [] | ||
current = head | ||
while current is not None: | ||
int_list.append(f"{current.data}") | ||
current = current.next | ||
return "->".join(int_list) | ||
|
||
|
||
def merge_lists(list_one: list, list_two: list) -> Node: | ||
""" | ||
>>> n1 = Node(1) | ||
>>> n2 = Node(2) | ||
>>> n3 = Node(3) | ||
>>> n4 = Node(4) | ||
>>> n5 = Node(5) | ||
>>> n6 = Node(6) | ||
>>> n1.next = n3 | ||
>>> n2.next = n4 | ||
>>> n4.next = n5 | ||
>>> n5.next = n6 | ||
>>> ll = merge_lists(n1, n2) | ||
>>> str_linked_list(ll) | ||
'1->2->3->4->5->6' | ||
""" | ||
node_one = list_one | ||
node_two = list_two | ||
merged_list = current_node = Node(0) | ||
|
||
while node_one is not None and node_two is not None: | ||
if node_one.data <= node_two.data: | ||
current_node.next = node_one | ||
node_one = node_one.next | ||
else: | ||
current_node.next = node_two | ||
node_two = node_two.next | ||
current_node = current_node.next | ||
|
||
if node_one is not None: | ||
current_node.next = node_one | ||
|
||
if node_two is not None: | ||
current_node.next = node_two | ||
|
||
return merged_list.next | ||
|
||
|
||
def main() -> None: | ||
list1 = [3, 9, 7, 5, 1] | ||
list2 = [4, 6, 5, 2, 8, 7, 10, 3, 0] | ||
ll1 = create_linked_list(list1) | ||
ll2 = create_linked_list(list2) | ||
ll3 = merge_lists(ll1, ll2) | ||
print(str_linked_list(ll3)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.