-
Notifications
You must be signed in to change notification settings - Fork 87
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
Caballero- SPRUCE #61
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you are missing the last two problems!
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(1) accessing it only on | ||
# Space Complexity: O(1) no new data structure being created | ||
def get_first(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: o(1) | ||
# Space Complexity: o(1) constant time, since new data structure is being created but its small | ||
def add_first(self, value): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
pass | ||
|
||
self.head = Node(value,self.head) | ||
|
||
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def search(self, value): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def search(self, value): | ||
pass | ||
current = self.head | ||
while current is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can shorten it to:
while current is not None: | |
while current: |
# Time Complexity: ? | ||
# Space Complexity: ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: 0(1) constant time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be O(n). The while loop is dependent upon the length of the linked list (ie, the input) to find the appropriate node
length += 1 | ||
current = current.next | ||
|
||
return length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this line out of the while loop; otherwise, it will return immediately after the first iteration:
return length | |
return length |
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns None if there are fewer nodes in the linked list than the index value | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_at_index(self, index): | ||
pass | ||
if self.length() < index: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love this! use those helper methods!
length += 1 | ||
current = current.next | ||
|
||
return length | ||
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns None if there are fewer nodes in the linked list than the index value | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_at_index(self, index): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great start, but if the index is less than the length of the linked list, how do we find the appropriate node value at that index?
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns None if there are fewer nodes in the linked list than the index value | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_at_index(self, index): | ||
pass | ||
if self.length() < index: | ||
return None | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns None if the linked list is empty | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_last(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we travel to the down to the last node and return its value?
No description provided.