-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Add merge sort for linked list #14712
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| Merge sort for a singly linked list. | ||
|
|
||
| https://en.wikipedia.org/wiki/Merge_sort | ||
| """ | ||
|
|
||
| from data_structures.linked_list.singly_linked_list import Node | ||
|
|
||
|
|
||
| def split_middle(node: Node) -> tuple[Node | None, Node | None]: | ||
| """ | ||
| Find the middle node of a linked list using the fast/slow pointer method. | ||
|
|
||
| Returns a tuple containing: | ||
| - the node before the middle | ||
| - the middle node | ||
| """ | ||
| fast = slow = node | ||
| previous: Node | None = None | ||
|
|
||
| while fast and fast.next_node: | ||
| fast = fast.next_node.next_node if fast.next_node else None | ||
| previous = slow | ||
| slow = slow.next_node | ||
|
|
||
| return previous, slow | ||
|
|
||
|
|
||
| def sort_list(head: Node | None) -> Node | None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added doctests for sort_list, along with a test function in the file. |
||
| """ | ||
| Sort a linked list using merge sort. | ||
| """ | ||
| if head is None: | ||
| return head | ||
|
|
||
| if head.next_node is None: | ||
| return head | ||
|
|
||
| previous, middle_node = split_middle(head) | ||
| if previous: | ||
| previous.next_node = None | ||
|
|
||
| left = head | ||
| right = middle_node | ||
|
|
||
| left = sort_list(left) | ||
| right = sort_list(right) | ||
|
|
||
| dummy_head = current = Node(0) | ||
|
|
||
| while left and right: | ||
| if left.data < right.data: | ||
| current.next_node = left | ||
| left = left.next_node | ||
| else: | ||
| current.next_node = right | ||
| right = right.next_node | ||
|
|
||
| current = current.next_node | ||
|
|
||
| while left: | ||
| current.next_node = left | ||
| next_node = left.next_node | ||
| left.next_node = None | ||
| left = next_node | ||
| current = current.next_node | ||
|
|
||
| while right: | ||
| current.next_node = right | ||
| next_node = right.next_node | ||
| right.next_node = None | ||
| right = next_node | ||
| current = current.next_node | ||
|
|
||
| return dummy_head.next_node | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/linked_list/merge_sort_linked_list.py, please provide doctest for the functionsplit_middleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added doctests for split_middle, along with a test function in the file.