Conversation
kyra-patton
left a comment
There was a problem hiding this comment.
🟡
| # Space Complexity: ? | ||
| # Time Complexity: constant, no traversing needed | ||
| # Space Complexity: constant, no new data structures | ||
| def get_first(self): |
| # Space Complexity: ? | ||
| # Time Complexity: constant | ||
| # Space Complexity: constant | ||
| def add_first(self, value): |
| # Space Complexity: ? | ||
| # Time Complexity: linear | ||
| # Space Complexity: constant | ||
| def search(self, value): |
| if self.head is None: | ||
| return False | ||
| current_node = self.head | ||
| target_node = Node(value) |
There was a problem hiding this comment.
You can create a new node for the target, but it's not necessary. See my suggestion on line 45 ⬇️
|
|
||
| # traverse list til reach target node or end of list | ||
| while current_node: | ||
| if current_node.value == target_node.value: |
There was a problem hiding this comment.
current_node.value is an integer, so instead of creating a new node target_node that has value value, you can just compare the current node's value directly to the passed in value.
| if current_node.value == target_node.value: | |
| if current_node.value == value: | |
| def add_last(self, value): | ||
| pass | ||
| if self.head is None: | ||
| self.head = value |
There was a problem hiding this comment.
🤔 self.head should be equal to a node object, not an integer like value
| new_tail_node = prev_node | ||
| current_node.next = new_tail_node | ||
| new_tail_node.next = None |
There was a problem hiding this comment.
Here, after creating the new_tail_node on line 115, you are overwriting it with prev_node which, once the while loop is finished executing, should be the current tail node.
Once the while loop is finished executing current_node is going to be the node after the the current tail, aka None.
Instead, try pointing the current tail (prev_node) to new_tail_node
| while current_node: | ||
| if current_node.value > max_node.value: | ||
| max_node = current_node | ||
| current_node = current_node.next |
There was a problem hiding this comment.
🤔You're creating an infinite loop here since you only reassign current_node to the next node in the list if current_node.value > max_node.value
| # Time Complexity: linear | ||
| # Space Complexity: constant |
There was a problem hiding this comment.
💫 Correct on the time and space complexity, however see my comments below ⬇️
| next_node = current_node.next | ||
| target_value = value | ||
| if current_node.value == target_value: | ||
| current_node = current_node.next |
There was a problem hiding this comment.
This makes current_node equal to what was the second node in the list, but it doesn't redirect self.head to point at the second node in the list
| current_node = current_node.next | |
| self.head = current_node.next | |
| # Space Complexity: ? | ||
| # Time Complexity: linear | ||
| # Space Complexity: linear | ||
| def visit(self): |
| # Space Complexity: ? | ||
| # Time Complexity: linear | ||
| # Space Complexity: constant | ||
| def reverse(self): |
Linked List