Skip to content
Open
Show file tree
Hide file tree
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
197 changes: 171 additions & 26 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
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 :prev # allow external entities to read or write next node

def initialize(value, next_node = nil)

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

Expand All @@ -18,71 +21,213 @@ 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 +24 to 26

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)
else
current = @head
first = Node.new(value)
first.next = current
current.prev = first
@head = first
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 +40 to 42

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
if @head.nil?
return false
else
while current.next != nil
if current.data == value
return true
else
current = current.next
end
end
end
if current.data == value
return true
else
return false
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 +65 to 67

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
max = -1000000
current = @head
if @head.nil?
return nil
elsif @head.next.nil?
return @head.data
else
while current.next != nil
if current.data > max
max = current.data
end
current = current.next
end
end

if current.data > max
max = current.data
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 +92 to 94

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
min = 1000000
current = @head
if @head.nil?
return nil
elsif @head.next.nil?
return @head.data
else
while current.next != nil
if current.data < min
min = current.data
end
current = current.next
end
end

if current.data < min
min = current.data
end

return min
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 +119 to 121

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
length = 1
current = @head
if current.nil?
return 0
elsif current.next.nil?
return 1
else
while current.next != nil
length +=1
current = current.next
end
end
return length
Comment on lines +122 to +134

Choose a reason for hiding this comment

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

This could be simplified a bit:

Suggested change
length = 1
current = @head
if current.nil?
return 0
elsif current.next.nil?
return 1
else
while current.next != nil
length +=1
current = current.next
end
end
return length
len = 0
current = @head
until current.nil?
len += 1
current = current.next
end
return len

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 +140 to 142

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
count = 0
if index > self.length-1
return nil
end

current = @head
if index == 0
return @head.data
else
while current.next != nil
current = current.next
count += 1
if count == index
return current.data
end
end
end
if index == self.length-1
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)
def visit
Comment on lines +166 to 168

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 != nil
puts current.value
current = current.next
end
puts current.value
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
if @head.nil?
return nil
end
current = @head
while current != nil
if @head.next.nil? && @head.data == value
@head = nil
elsif @head.next != nil && @head.data == value
@head = @head.next
elsif current.next != nil && current.prev != nil && current.data == value
prevn = current.prev
nextn = current.next
prevn.next = nextn
nextn.prev = prevn
elsif !current.prev.nil? && current.data == value
current.prev.next = current.next
end
current = current.next
end
Comment on lines +184 to +199

Choose a reason for hiding this comment

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

This is a bit overcomplicated

Suggested change
current = @head
while current != nil
if @head.next.nil? && @head.data == value
@head = nil
elsif @head.next != nil && @head.data == value
@head = @head.next
elsif current.next != nil && current.prev != nil && current.data == value
prevn = current.prev
nextn = current.next
prevn.next = nextn
nextn.prev = prevn
elsif !current.prev.nil? && current.data == value
current.prev.next = current.next
end
current = current.next
end
if @head.data == value
@head = @head.next
return
end
current = @head
previous = nil
until current.nil?
if current.data == value
previous.next = current.next
return
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: O(N)
# Space Complexity: O(1)
def reverse
Comment on lines +204 to 206

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.next.nil?
return @head
end

current = @head

while current.next != nil
current = current.next
end

# current = current.next
@head = current

while current != nil
prev = current.prev
current.prev = current.next
current.next = prev
current = prev
end

# if temp != nil
# @head = temp.prev
# end
return @head
end


Expand Down
2 changes: 1 addition & 1 deletion test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,4 @@
expect(@list.get_at_index(3)).must_equal 1
end
end
end
end