Skip to content

Add merge sort for linked list#14712

Open
F-Blaze wants to merge 3 commits into
TheAlgorithms:masterfrom
F-Blaze:merge-sort-linked-list
Open

Add merge sort for linked list#14712
F-Blaze wants to merge 3 commits into
TheAlgorithms:masterfrom
F-Blaze:merge-sort-linked-list

Conversation

@F-Blaze
Copy link
Copy Markdown

@F-Blaze F-Blaze commented May 22, 2026

Describe your change:

  • Add an algorithm

This pull request adds a merge sort implementation for a singly linked list in data_structures/linked_list/merge_sort_linked_list.py.

The implementation includes:

  • splitting the linked list into two halves
  • recursively sorting each half
  • merging two sorted linked lists
  • type hints
  • doctest examples
  • a Wikipedia reference

Checklist:

  • I have read CONTRIBUTING.md
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.

@algorithms-keeper algorithms-keeper Bot added the require tests Tests [doctest/unittest/pytest] are required label May 22, 2026
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

from data_structures.linked_list.singly_linked_list import Node


def split_middle(node: Node) -> tuple[Node | None, Node | None]:
Copy link
Copy Markdown

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 function split_middle

Copy link
Copy Markdown
Author

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.

return previous, slow


def sort_list(head: Node | None) -> Node | None:
Copy link
Copy Markdown

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 function sort_list

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label May 22, 2026
@algorithms-keeper algorithms-keeper Bot removed the require tests Tests [doctest/unittest/pytest] are required label May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant