Skip to content
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: 126 additions & 35 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,95 +18,186 @@ 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)
Comment on lines +21 to 23

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
new_node = Node.new(value, @head)
@head = new_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: O(n) - Must traverse list
# Space Complexity: O(1)
def search(value)
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
while current
return true if value == current.data
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: O(n) - Must traverse list
# Space Complexity: O(1)
def find_max
Comment on lines +44 to 46

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
max = current&.data

Choose a reason for hiding this comment

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

Why the &??

Copy link
Author

Choose a reason for hiding this comment

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

This is to avoid an error stating that there is no such method .data for nil

while current
current = current.next
if current&.data && current.data > max
max = current.data
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: O(n) - Must traverse list
# Space Complexity: O(1)
def find_min
Comment on lines +61 to 63

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
end
current = @head
min = current&.data
while current
current = current.next
if current&.data && current.data < min
min = current.data
end
end

return min
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
Comment on lines +79 to 81

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil unless @head
return @head.data
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - Must traverse list
# Space Complexity: O(1)
def add_last(value)
Comment on lines +87 to 89

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
unless current
@head = Node.new(value)
return true
end

while current&.next
current = current.next
end

current.next = Node.new(value)
end

# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - Must traverse list
# Space Complexity: O(1)
def length
Comment on lines +104 to 106

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
count = 0
current = @head
while current
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: O(n) - Must traverse list
# Space Complexity: O(1)
def get_at_index(index)
Comment on lines +120 to 122

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
count = 0
current = @head
while current && count < index
current = current.next
count += 1
end

return current&.data
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - Must traverse list
# Space Complexity: O(1)
def visit
Comment on lines +134 to 136

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
count = 0
current = @head
while current && count < index
pp "Node #{count} is #{current.data}"
current = current.next
count += 1
end
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def delete(value)
Comment on lines +147 to 149

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
if @head&.data == value
@head = @head&.next
return true
end

first = @head
second = first&.next

while second
if second.data == value
first.next = second.next
return true
end

first = first&.next
second = second.next
end

return false
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: O(n)
# Space Complexity: O(1)
def reverse
Comment on lines +173 to 175

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
first = nil
second = @head

while second
third = second&.next
second.next = first
first = second
second = third
end

@head = first

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: O(n)
# Space Complexity: O(1)
def get_last
Comment on lines +192 to 194

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
while current.next
current = current.next
end

return current&.data
end

## Advanced Exercises
Expand Down