Skip to content

Added reverse_linked_list.py #4973

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

Closed
wants to merge 2 commits into from
Closed

Conversation

immiel
Copy link
Contributor

@immiel immiel commented Oct 4, 2021

I looked around and noticed that there's no algorithm that reverse a linked list and returns them. The closest I could find prints them instead of returning them as a function. And so, here it is.

The function only have one input and one output. The function works as follows; it pops data from the beginning of input and append them to the beginning of output, and this is performed until there's no more node in input.

For tests, I added doctests for short and concise explanation as to how to use the function, and additional tests if the code is run independently. Hopefully, this piece of code and its documentation could be a suggestion for someone that needs to know how to reverse a linked list.

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

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 have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

I looked around and noticed that there's no algorithm that reverse a linked list and returns them. The closest I could find prints them instead of returning them as a function. And so, here it is.

The function only have one input and one output. The function works as follows; it pops data from the beginning of input and append them to the beginning of output.

For tests, I added doctests for short and concise explanation as to how to use the function, and additional tests if the code is run independently. Hopefully, this piece of code and its documentation could be a suggestion for someone that needs to know how to reverse a linked list.
@ghost ghost added awaiting reviews This PR is ready to be reviewed require tests Tests [doctest/unittest/pytest] are required labels Oct 4, 2021
Copy link

@ghost ghost 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.

self.item = item
self.next = next

def __repr__(self) -> str:
Copy link

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/reverse_linked_list.py, please provide doctest for the function __repr__

@ghost ghost removed the require tests Tests [doctest/unittest/pytest] are required label Oct 4, 2021
@poyea
Copy link
Member

poyea commented Oct 4, 2021

Actually there's one: https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/singly_linked_list.py#L146. However, you can always improve the current version by adding tests (nice test cases that you have) and type hints.

@poyea
Copy link
Member

poyea commented Oct 4, 2021

So you can close this and open a new one, or update the current singly_linked_list.py in this PR. I think both would work.

@poyea poyea self-assigned this Oct 4, 2021
@immiel
Copy link
Contributor Author

immiel commented Oct 4, 2021

So you can close this and open a new one, or update the current singly_linked_list.py in this PR. I think both would work.

Alright. I guess I'll close this one and open a new one to update that one. Thank you for the help!

@immiel immiel closed this Oct 4, 2021
immiel added a commit to immiel/Python that referenced this pull request Oct 4, 2021
This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.
immiel added a commit to immiel/Python that referenced this pull request Oct 4, 2021
This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
immiel added a commit to immiel/Python that referenced this pull request Oct 4, 2021
This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
immiel added a commit to immiel/Python that referenced this pull request Oct 4, 2021
This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
immiel added a commit to immiel/Python that referenced this pull request Oct 4, 2021
This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
immiel added a commit to immiel/Python that referenced this pull request Oct 4, 2021
This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
poyea pushed a commit that referenced this pull request Oct 20, 2021
…st cases (#4988)

This is a followup to #4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
@immiel immiel deleted the patch-4 branch October 20, 2021 09:59
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
…st cases (TheAlgorithms#4988)

This is a followup to TheAlgorithms#4973 (comment)

As per given suggestion, I've added type hints to certain methods that don't have them.  I have also added documentation and example doctests as a usage example for (most of) those that don't have them.

I have also added another test case following the previous test case's format. I noticed that the existing test case from previous pull request might be redundant with the ones I've made, so I decided to create a specific situation where the linked list would have to keep different kinds of data types for each node, in `test_singly_linked_list_2` test function.

Some minor changes in strings has been done to keep things consistent with other parts of the document. If it is undesirable, please let me know.
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.

2 participants