Added an add at position subroutiune to linked list#9020
Added an add at position subroutiune to linked list#9020tianyizheng02 merged 14 commits intoTheAlgorithms:masterfrom
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.
| if current: # Check if current is not None | |
| if current is not None: |
| else: | ||
| return False |
There was a problem hiding this comment.
| else: | |
| return False | |
| return False |
| if position < 0: | ||
| return False |
There was a problem hiding this comment.
Maybe this should raise an error?
| if position < 0: | |
| return False | |
| if 0 > position: | |
| return False |
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?
|
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 |
CONTRIBUTING.md
Outdated
| @@ -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
|
added the doctest and updated the else code after checking the position is 0 or not |
CONTRIBUTING.md
Outdated
| @@ -1,4 +1,4 @@ | |||
| # Contributing guidelines | |||
| <!-- # Contributing guidelines --> | |||
There was a problem hiding this comment.
| <!-- # Contributing guidelines --> | |
| # Contributing guidelines |
Then please uncomment this line
|
fixed contributing.md and added the doctest for checing out of bounds posiiton, both positive and negative |
|
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: