-
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?
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -13,47 +13,63 @@ def __init__(self): | |||||
|
||||||
# returns the value in the first node | ||||||
# returns None if the list is empty | ||||||
# 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): | ||||||
pass | ||||||
if self.head == None: | ||||||
return None | ||||||
|
||||||
return self.head.value | ||||||
|
||||||
# method to add a new node with the specific data value in the linked list | ||||||
# insert the new node at the beginning of the linked list | ||||||
# 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 commentThe 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: ? | ||||||
Comment on lines
33
to
34
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 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? |
||||||
def search(self, value): | ||||||
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. 👍 |
||||||
pass | ||||||
current = self.head | ||||||
while 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. we can shorten it to:
Suggested change
|
||||||
if value == current.value: | ||||||
return True | ||||||
current = current.next | ||||||
# reset current to equal the next node in the list | ||||||
return False | ||||||
|
||||||
# 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 commentThe 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 |
||||||
# Space Complexity: 0(1) | ||||||
def length(self): | ||||||
pass | ||||||
current = self.head | ||||||
length = 0 | ||||||
|
||||||
while current != None: | ||||||
length += 1 | ||||||
current = current.next | ||||||
|
||||||
return length | ||||||
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. Move this line out of the while loop; otherwise, it will return immediately after the first iteration:
Suggested change
|
||||||
|
||||||
# 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 commentThe 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? |
||||||
pass | ||||||
if self.length() < index: | ||||||
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. love this! use those helper methods! |
||||||
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 commentThe 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? |
||||||
pass | ||||||
|
||||||
# method that inserts a given value as a new last node in the linked list | ||||||
# 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.
👍