-
Notifications
You must be signed in to change notification settings - Fork 46
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
Leaves - Julia K #24
base: master
Are you sure you want to change the base?
Leaves - Julia K #24
Conversation
…ing sense so far.
…ast. Considered changing test names to snake case -- i.e. add_last instead of addLast -- but decided it's unnecessary in this situation. Glad to see I'm not the only one who mixes up Ruby and JavaScript conventions.
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.
Nice work, you hit the learning goals here. That said you have some inefficiencies here with how you used the length
method in some of your solutions. Take a look at my comments and let me know what questions you have.
@@ -1,45 +1,95 @@ | |||
require_relative 'node' | |||
require 'pry' |
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.
In the future please remove pry before submitting.
# Space complexity - ? | ||
|
||
# Time complexity - O(1) because only one operation will be performed regardless of how many nodes the list has | ||
# Space complexity - Also O(1) | ||
def add_first(data) |
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.
👍
# Space complexity - ? | ||
|
||
# Time complexity - Also O(1) because again, only one operation is being performed regardless of list length | ||
# Space complexity - O(1) | ||
def get_first |
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.
👍
# Space complexity - ? | ||
|
||
# Time complexity - O(n) because we are performing an operation for every node in the list | ||
# Space complexity - I think it's constant, because although we are assigning current and count variable values for each node in the list, we are overwriting existing variables instead of creating new variables each time, meaning we're just re-using the same places in memory. | ||
def 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.
👍
# Space complexity - ? | ||
|
||
# Time complexity - O(n) because the only way we can get to the end of the list is by manually moving through each list node | ||
# Space complexity - Constant because we are re-using the same variables instead of creating new variables for each node. | ||
def add_last(data) |
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.
👍
if length == 0 | ||
add_first(data) | ||
else | ||
# use length method to move to the last node in the list | ||
(length - 1).times do |
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.
Just note that since length
is a method, you're calling it twice here, potentially, so this becomes a runtime of 3n instead of n. It doesn't change the Big-O, but it's a bit inefficient.
# Space complexity - ? | ||
|
||
# Time complexity - Same as add_last | ||
# Space complexity - Same as add_last | ||
def get_last |
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.
Similar issues with the above. Can you think of a simple way to make this more efficient?
# initialize current to head | ||
current = @head | ||
# account for index being greater than list length | ||
if index + 1 > 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.
🥇
# Space complexity - ? | ||
|
||
# Time complexity - O(i) where i is the index | ||
# Space complexity - constant | ||
def get_at_index(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.
👍
Linked List Comprehension Questions
ArrayLinked List more advantageous?