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
212 changes: 174 additions & 38 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
class Node
attr_reader :data # allow external entities to read value but not write
attr_accessor :next # allow external entities to read or write next node
attr_accessor :previous

def initialize(value, next_node = nil)
def initialize(value, next_node = nil, prev_node = nil)
@data = value
@next = next_node
@previous = prev_node
end
end

Expand All @@ -18,71 +20,185 @@ 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 +23 to 25

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 = @tail = Node.new(value)
else
new_node = Node.new(value)
new_node.next = @head
@head.previous = new_node
@head = new_node
end
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 +38 to 40

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current_node = @head

until current_node.nil?
return true if current_node.data == value

current_node = current_node.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)
# Space Complexity: O(1)
def find_max
Comment on lines +54 to 56

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current_node = @head
max_value = current_node.data unless current_node.nil?

until current_node.nil?
max_value = current_node.data if current_node.data > max_value

current_node = current_node.next
end

return max_value
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 +71 to 73

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current_node = @head
min_value = current_node.data unless current_node.nil?

until current_node.nil?
min_value = current_node.data if current_node.data < min_value

current_node = current_node.next
end

return min_value
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 +88 to 90

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current_node = @head
length = 0

until current_node.nil?
length += 1

current_node = current_node.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 +106 to 108

Choose a reason for hiding this comment

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

👍 Good use of your length method

raise NotImplementedError
length = self.length
return nil if length <= index
return @head.data if index == 0

current_index = 0
current_node = @head

until current_index == index
current_index += 1
current_node = current_node.next
end

return current_node.data
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def visit
Comment on lines +125 to 127

Choose a reason for hiding this comment

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

👍 , but it's O(1) space complexity

raise NotImplementedError
return nil if @head.nil?
current_node = @head

until current_node.nil?
puts current_node.data

current_node = current_node.next
end

return
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)
raise NotImplementedError
return if @head.nil?

if @head.data == value
@head = nil if @head.next.nil?
@head = @head.next unless @head.nil?
return
end
Comment on lines +146 to +150

Choose a reason for hiding this comment

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

This can be simpler, but since you have a doubly linked list you also need to set the previous.

Suggested change
if @head.data == value
@head = nil if @head.next.nil?
@head = @head.next unless @head.nil?
return
end
if @head.data == value
@head = @head.next
@head.previous = nil
return
end


current_node = @head
previous_node = @head.previous

Choose a reason for hiding this comment

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

Since @head.previous is always going to be nil.

Suggested change
previous_node = @head.previous
previous_node = nil


until current_node.nil?
if current_node.next.nil?

Choose a reason for hiding this comment

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

Here you also need to verify that current_node is the node you need to delete.

@tail = previous_node
previous_node.next = nil
return
end

next_node = current_node.next

if current_node.data == value

next_node.previous = previous_node
previous_node.next = next_node
@tail = next_node if next_node.next.nil?
return
end

previous_node = current_node
current_node = current_node.next
end

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

Choose a reason for hiding this comment

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

Space complexity is O(1) and time complexity is O(n)

raise NotImplementedError
return if @head.nil?
return if @head.next.nil?

temp = @head
@head = temp
Comment on lines +187 to +188

Choose a reason for hiding this comment

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

huh? temp is head and head is temp?

@tail = @head

current = @head

until current == nil
temp = current.next
current.next = current.previous
current.previous = temp

current = current.previous
end

Choose a reason for hiding this comment

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

You need to adjust the @head here.

return
end


Expand All @@ -96,10 +212,23 @@ def find_middle_value

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_nth_from_end(n)
Comment on lines +215 to 217

Choose a reason for hiding this comment

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

👍 , good use of length.

raise NotImplementedError
length = self.length
return nil if length <= n
return @head.data if n == 0
return @tail.data if n == length - 1

current_index = length - 1
current_node = @tail

until current_index == n
current_index -= 1
current_node = current_node.previous
end

return current_node.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -115,25 +244,32 @@ def has_cycle
# 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
return @head.data unless @head.nil?
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def add_last(value)
Comment on lines +254 to 256

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 = @tail = Node.new(value)
else
new_node = Node.new(value)
@tail.next = new_node
new_node.previous = @tail
@tail = new_node
end
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(1)
# Space Complexity: O(1)
def get_last
Comment on lines +269 to 271

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @tail.data unless @tail.nil?
end

# method to insert a new node with specific data value, assuming the linked
Expand Down