Uh oh!
There was an error while loading. Please reload this page.
Added an add at position subroutiune to linked list - #9020
Conversation
| >>> linked_list.add(3) | ||
| >>> linked_list.add_at_position(10, 1) | ||
| True | ||
There was a problem hiding this comment.
Here there should be a test displaying linked_list
| current = current.next | ||
| counter += 1 | ||
| if current: # Check if current is not None |
There was a problem hiding this comment.
| ifcurrent: # Check if current is not None | |
| ifcurrentisnotNone: |
| else: | ||
| return False |
There was a problem hiding this comment.
| else: | |
| returnFalse | |
| returnFalse |
| if position < 0: | ||
| return False |
There was a problem hiding this comment.
Maybe this should raise an error?
| ifposition<0: | |
| returnFalse | |
| if0>position: | |
| returnFalse |
This also looks a bit more readable
| self.head = Node(item, self.head) | ||
| self.size += 1 | ||
| def add_at_position(self, item: Any, position: int) -> bool: |
There was a problem hiding this comment.
What about insert_at_position?
Uh oh!
There was an error while loading. Please reload this page.
mendacium-a11y
commented
Sep 6, 2023
updated the the code by dropping the add_at_position, instead updated the add function to take an optional position argument to specify of we want to insert the node at a specific position in the linked list |
| @@ -1,4 +1,4 @@ | |||
| # Contributing guidelines | |||
| <!-- # Contributing guidelines --> | |||
There was a problem hiding this comment.
Don't edit the contributing guidelines
There was a problem hiding this comment.
this seems to have happened by mistake, didn't intend to
There was a problem hiding this comment.
| <!--# Contributing guidelines--> | |
| # Contributing guidelines |
Then please uncomment this line
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
mendacium-a11y
commented
Sep 7, 2023
added the doctest and updated the else code after checking the position is 0 or not |
Uh oh!
There was an error while loading. Please reload this page.
| @@ -1,4 +1,4 @@ | |||
| # Contributing guidelines | |||
| <!-- # Contributing guidelines --> | |||
There was a problem hiding this comment.
| <!--# Contributing guidelines--> | |
| # Contributing guidelines |
Then please uncomment this line
mendacium-a11y
commented
Sep 8, 2023
fixed contributing.md and added the doctest for checing out of bounds posiiton, both positive and negative |
tianyizheng02
commented
Sep 8, 2023
Thanks for your contribution! |
Describe your change:
This pull request adds the
add_at_positionfunction to the linked list implementation. This function allows inserting an item at a specified position within the linked list.Checklist: