Conversation
spitsfire
left a comment
There was a problem hiding this comment.
You are missing a lot of your space/time complexity explanations
| # Space Complexity: ? | ||
| # Time Complexity: O(1)? | ||
| # Space Complexity: O(1)? | ||
| def get_first(self): |
There was a problem hiding this comment.
resubmitted with questions filled out
| # insert the new node at the beginning of the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def add_first(self, value): |
| pass | ||
| current = self.head | ||
|
|
||
| while current != None: |
There was a problem hiding this comment.
We can shorten this to:
| while current != None: | |
| while current: |
| current = self.head | ||
|
|
||
| count = 0 | ||
| while current != None: |
There was a problem hiding this comment.
| while current != None: | |
| while current: |
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def length(self): |
| while current is not None: | ||
| if current.value == value: | ||
| break | ||
| prev = current | ||
| current = current.next | ||
|
|
||
| if current == None: | ||
| return None | ||
|
|
||
| prev.next = current.next | ||
|
|
||
| current = None |
There was a problem hiding this comment.
We can shorten this, too, to make it more readable:
| while current is not None: | |
| if current.value == value: | |
| break | |
| prev = current | |
| current = current.next | |
| if current == None: | |
| return None | |
| prev.next = current.next | |
| current = None | |
| while current is not None: | |
| if current.next.value == value: | |
| current.next = current.next.next | |
| current = current.next |
We don't need to re-assign current to None nor return None
| # Space Complexity: ? | ||
| # Time Complexity: O(n)? | ||
| # Space Complexity: O(1)? | ||
| def reverse(self): |
| current = self.head | ||
| count = 0 | ||
| while current: | ||
| count += 1 | ||
| current = current.next |
There was a problem hiding this comment.
hmm where have we seen this already? Maybe we can call length to get the number of nodes
| # returns true if a cycle is found, false otherwise. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def has_cycle(self): |
linked_list/linked_list.py
Outdated
| # Time Complexity: ? | ||
| # Space Complexity: ? |
There was a problem hiding this comment.
What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?
|
Resubmitted with time complexity |
No description provided.