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
134 changes: 102 additions & 32 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,145 @@ 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
@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 +29 to 31

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
while !current.nil? # while the current node is not nil
if current.data == value
return true
else
current = current.next
end
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)
# Space Complexity: O(1)
def find_max
Comment on lines +46 to 48

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
current = @head
max = 0
while current != nil
if current.data > max
max = current.data
end
current = current.next
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)
# Space Complexity: O(1)
def find_min
Comment on lines +63 to 65

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
current = @head
min = @head.data
while current != nil
if current.data < min
min = current.data
end
current = current.next
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 +82 to 84

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
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)
# Space Complexity: O(1)
def add_last(value)
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.

👍

raise NotImplementedError
if @head == nil
@head = Node.new(value, nil)
return
end

current = @head
until current.next == nil
current = current.next
end
current.next = Node.new(value, nil)
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return 0 if @head == nil
length = 0
current = @head
until current.nil?
length += 1
current = current.next
end
return length
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)
# Space Complexity: O(1)
def get_at_index(index)
Comment on lines +122 to 124

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 index >= self.length
current = @head
count = 0
if @head.nil? == false
until count == index
count += 1
current = current.next
end
return current.data
end
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1) because each value is printed to console, never saved to a data structure?
def visit
Comment on lines +138 to 140

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
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
# def delete(value)
# return nil if @head == nil
# if @head.data == value
# @head = @head.next

# end

# end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
Expand All @@ -103,12 +168,17 @@ def reverse

# 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 +171 to 173

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
current = @head
until current.next == nil
current = current.next
end
return current.data
end

#___________________________________________________________________
## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
Expand Down