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

Carla #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 123 additions & 38 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -18,73 +18,150 @@ def initialize

# 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)
def add_first(value)
raise NotImplementedError
node = Node.new(value)
if @head != nil

Choose a reason for hiding this comment

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

Does it matter if @head is nil?

node.next = @head
end
@head = node
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def search(value)
raise NotImplementedError
current = @head
return false if !current.next

Choose a reason for hiding this comment

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

What about if value was at the head?


while current.next != nil

Choose a reason for hiding this comment

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

What if the value is in the last node, where it's next is nil?

return true if current.data == value
current = current.next
end
return false
end

# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def find_max
raise NotImplementedError
return if @head.nil?
current = @head
max = nil

Choose a reason for hiding this comment

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

It would be better if you set max = @head.data

while current != nil
if current.data > max
max = current.data
else

Choose a reason for hiding this comment

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

Do you need an else here?

current = current.next
end
end
return max
end

# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def find_min
raise NotImplementedError
return if @head.nil?
current = @head
min = nil

while current != nil
if current.data < min
min = current.data
else
current = current.next
end
end
return min
end


# method that returns the length of the singly linked list
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def length
raise NotImplementedError
count = 1
current = @head
return 0 if current == nil
while current.next != nil
count += 1
current = current.next
end
return count
end

# method that returns the value at a given index in the linked list
# index count starts at 0
# returns nil if there are fewer nodes in the linked list than the index value
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def get_at_index(index)
raise NotImplementedError
i = 0
current = @head
while current != nil
if i == index
return current.data
elsif i > index
return nil
end
current = current.next
i += 1
end
end

# method to print all the values in the linked list
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: linear
def visit
raise NotImplementedError
current = @head
while current != nil
puts current.data
end
current = current.next
end

# method to delete the first node found with specified value
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def delete(value)
raise NotImplementedError
return if !@head

Choose a reason for hiding this comment

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

There's a test failing for this, but that's because the test uses add_last


previous = nil
current = @head

while current != nil
if current.data == value
if @head.data == value

Choose a reason for hiding this comment

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

Does this @head.data == value need to be in the loop?

@head = current.next
else
previous.next = current.next
end
end
previous = current
current = current.next
end
end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def reverse

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
previous = nil

while current
next_node = current.next
current.next = previous
previous = current
current = next_node
end
@head = previous
end


@@ -93,15 +170,15 @@ def reverse
# Time Complexity:
# Space Complexity
def find_middle_value
raise NotImplementedError
# raise NotImplementedError
end

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity:
# Space Complexity
def find_nth_from_end(n)
raise NotImplementedError
# raise NotImplementedError
end

# checks if the linked list has a cycle. A cycle exists if any node in the
@@ -110,40 +187,48 @@ def find_nth_from_end(n)
# Time Complexity:
# Space Complexity
def has_cycle
raise NotImplementedError
# raise NotImplementedError
end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity:
# Space Complexity
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_first
raise NotImplementedError
if [email protected]?
return @head.data
end
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity:
# Space Complexity
def add_last(value)
raise NotImplementedError
# raise NotImplementedError
end

# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
# Time Complexity:
# Space Complexity
# Time Complexity: linear
# Space Complexity: O(1)
def get_last
raise NotImplementedError
return if @head.nil?

current = @head
while current.next != nil
current = current.next
end
return current.data
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity:
# Space Complexity
def insert_ascending(value)
raise NotImplementedError
# raise NotImplementedError
end

# Helper method for tests