-
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
Lilly C16 Pine #64
base: master
Are you sure you want to change the base?
Lilly C16 Pine #64
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.
You are missing a lot of your space/time complexity explanations
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(1)? | ||
# Space Complexity: O(1)? | ||
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.
👍
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.
resubmitted with questions filled out
|
||
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: ? | ||
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 | ||
current = self.head | ||
|
||
while current != 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 this to:
while current != None: | |
while current: |
current = self.head | ||
|
||
count = 0 | ||
while current != 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.
while current != None: | |
while current: |
if current.value == value: | ||
return True | ||
current = current.next | ||
return False | ||
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def length(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.
👍
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n)? | ||
# Space Complexity: O(1)? | ||
def reverse(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.
👍
current = self.head | ||
count = 0 | ||
while current: | ||
count += 1 | ||
current = current.next |
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.
hmm where have we seen this already? Maybe we can call length
to get the number of nodes
return current.value | ||
|
||
|
||
|
||
|
||
# checks if the linked list has a cycle. A cycle exists if any node in the | ||
# linked list links to a node already visited. | ||
# returns true if a cycle is found, false otherwise. | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def has_cycle(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.
👍 interesting approach!
linked_list/linked_list.py
Outdated
# 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?
Resubmitted with time complexity |
No description provided.