Skip to content
71 changes: 56 additions & 15 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,70 @@ 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.

👍 , very compact!

raise NotImplementedError

@head = Node.new(value, @head)

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)?
# Space Complexity: O(1)?
def search(value)
Comment on lines +31 to 33

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return false if @head == nil
current = @head

until current == 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
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
return nil if @head == nil

max = @head
current = @head

until current == nil
current.data > max.data ? max = current : current = current.next
end
return max.data
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)?
# Space Complexity: O(1)?
def find_min
Comment on lines +60 to 62

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @head == nil
min = @head
current = @head

until current == nil
current.data < min.data ? min = current : current = current.next
end
return min.data
end


# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def length
Comment on lines 75 to 77

Choose a reason for hiding this comment

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

👍 , time/space complexity ❓

raise NotImplementedError
count = 0
current = @head
until current == nil
count += 1
current = current.next
end
return count
end

# method that returns the value at a given index in the linked list
Expand All @@ -60,29 +90,40 @@ def length
# Time Complexity: ?
# Space Complexity: ?
def get_at_index(index)
Comment on lines 90 to 92

Choose a reason for hiding this comment

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

👍 With one caveat below

raise NotImplementedError
return nil if @head.nil?

current = @head
index.times do
current = current.next
end
Comment on lines +96 to +98

Choose a reason for hiding this comment

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

What if index >= length?

return current.data
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
def visit
Comment on lines 103 to 105

Choose a reason for hiding this comment

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

👍 Time/space complexity ❓

raise NotImplementedError
current = @head

until current == nil
puts current.data
current = current.next
end
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
def delete(value)
raise NotImplementedError

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: ?
def reverse
raise NotImplementedError

end


Expand Down