Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Leaves - Julia K #24

wants to merge 5 commits into from

Conversation

Kalakalot
Copy link

Linked List Comprehension Questions

Question Response
1. What advantages does a LinkedList have over an Array? Adding to and removing from the front of list is more efficient -- O(1) as opposed to O(n) -- so inserting and deleting can be much faster. Linked lists are also more memory efficient since nodes don't have to be next to each other in memory.
2. When is an Array more advantageous? Arrays seem much handier when efficiency isn't super important and you want to use built-in methods to transform data. Also, if you are accessing elements via index, lookup is a constant-time operation (unlike in linked lists, where index lookup is O(i) where i is the index.)
3. When is an Array Linked List more advantageous? If you want to conserve memory and maximize time efficiency, linked lists are a better choice since you don't need contiguous memory space for each element, memory is assigned dynamically, deleting is almost always faster, and adding can also be much faster.

Julia A Kingrey added 5 commits December 7, 2019 12:41
…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.
Copy link

@CheezItMan CheezItMan left a 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'

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)

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

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

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +46 to +50
if length == 0
add_first(data)
else
# use length method to move to the last node in the list
(length - 1).times do

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

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

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants