-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added an add at position subroutiune to linked list #9020
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
Changes from 2 commits
8e8bea6
a6fbb44
e00d58e
823da1b
142d318
a78cda0
b4485c5
6623820
8b33a4b
a07cf7f
0aa274b
4a32c1f
46cbb2b
542d795
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,41 @@ def add(self, item: Any) -> None: | |||||||||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||||||||||
""" | ||||||||||
Adds a new node with the given item at the specified position in the linked list | ||||||||||
|
||||||||||
Args: | ||||||||||
item (Any): The item to be added to the linked list. | ||||||||||
position (int): The position at which the item should be inserted. | ||||||||||
|
||||||||||
Returns: | ||||||||||
bool: True if the insertion was successful, False otherwise. | ||||||||||
|
||||||||||
>>> linked_list = LinkedList() | ||||||||||
>>> linked_list.add(1) | ||||||||||
>>> linked_list.add(2) | ||||||||||
>>> linked_list.add(3) | ||||||||||
>>> linked_list.add_at_position(10, 1) | ||||||||||
True | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here there should be a test displaying |
||||||||||
""" | ||||||||||
if position < 0: | ||||||||||
return False | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should raise an error?
Suggested change
This also looks a bit more readable |
||||||||||
|
||||||||||
current = self.head | ||||||||||
counter = 0 | ||||||||||
while current and counter < position: | ||||||||||
current = current.next | ||||||||||
counter += 1 | ||||||||||
|
||||||||||
if current: # Check if current is not None | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
new_node = Node(item, current.next) | ||||||||||
current.next = new_node | ||||||||||
return True | ||||||||||
else: | ||||||||||
return False | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
def remove(self) -> Any: | ||||||||||
# Switched 'self.is_empty()' to 'self.head is None' | ||||||||||
# because mypy was considering the possibility that 'self.head' | ||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.