Skip to content

Fix merge_sort_linked_list crashing on equal values - #15794

Open
JayPokale wants to merge 2 commits into
TheAlgorithms:masterfrom
JayPokale:fix-merge-sort-linked-list-duplicates
Open

JayPokale wants to merge 2 commits into
TheAlgorithms:masterfrom
JayPokale:fix-merge-sort-linked-list-duplicates

Conversation

@JayPokale

@JayPokale JayPokale commented Nov 24, 2026 •

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

data_structures/linked_list/merge_sort_linked_list.py::merge compared whole nodes with left <= right. Node is a @dataclass(order=True), so that compares (data, next) tuples. When the two heads hold equal values, Python goes on to compare their next fields, and as soon as one of them is None it raises TypeError: '<=' not supported between instances of 'Node' and 'NoneType'. So merge_sort_linked_list crashes on most lists with repeated values, for example 1 -> 1 -> 1 or 2 -> 1 -> 2 -> 1 -> 2. Where it did not crash it was also walking the rest of both lists to break ties. merge now compares left.data <= right.data, which is what the function means and keeps the sort stable.

I added a doctest with repeated values (2 -> 1 -> 2 -> 1 -> 2); it fails on master with the TypeError, and I kept it in this PR as the regression evidence for this one-line fix. Over 400 random lists of up to 9 values drawn from 0..6 the sorted result equals sorted(values) after the change (three seeds); on master, 111 of the 400 lists raised with seed 1 and 135 with seed 2 (I did not run seed 3 on master). ruff check, ruff format --check and python -m doctest pass.

Written with the help of Claude Code (Claude Sonnet 5.5).

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. To ease review, please open separate PRs for separate algorithms.
  • 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.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant