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
337 changes: 223 additions & 114 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,161 +1,270 @@
# frozen_string_literal: true

# Defines a node in the singly linked list
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)
@data = value
@next = next_node
@previous = previous
end
end

# Defines the singly linked list
class LinkedList
def initialize
@head = nil # keep the head private. Not accessible outside this class
end
def initialize
@head = nil # keep the head private. Not accessible outside this class
@tail = nil
end

# 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: ?
def add_first(value)
raise NotImplementedError
# 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:O(1)
# Space Complexity:O(1)
def add_first(value)
Comment on lines +25 to +27

Choose a reason for hiding this comment

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

👍

if @head.nil?
@head = @tail = Node.new(value)
else
new_node = Node.new(value, @head)
@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: ?
def search(value)
raise NotImplementedError
end
# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity: O(n)
# Space Complexity: O(n)
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.

👍 , the space complexity is O(1)

return false if @head.nil?

# method to return the max value in the linked list
# returns the data value and not the node
def find_max
raise NotImplementedError
end
current = @head
return true if current.data == value

# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity: ?
# Space Complexity: ?
def find_min
raise NotImplementedError
while (current = current.next)

Choose a reason for hiding this comment

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

Clever use of the = sign here.

return true if current.data == value
end
false
end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max
return nil if @head.nil?

# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def length
raise NotImplementedError
end
max = @head.data
current = @head

# 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: ?
def get_at_index(index)
raise NotImplementedError
while (current = current.next)
max = current.data if current.data > max
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
def visit
raise NotImplementedError
end
max
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
def delete(value)
raise NotImplementedError
end
# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity: O(n)
# Space Complexity: O(n)
def find_min
Comment on lines +69 to +71

Choose a reason for hiding this comment

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

👍 , the space complexity is O(1) because you're not creating a new list.

return nil if @head.nil?

# 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
min = @head.data
current = @head
while (current = current.next)
min = current.data if current.data < min
end
min
end

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

Choose a reason for hiding this comment

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

👍 , the space complexity is O(1) because you're not creating a new list.

return 0 if @head.nil?

## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def find_middle_value
raise NotImplementedError
end
count = 1
current = @head

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: ?
# Space Complexity: ?
def find_nth_from_end(n)
raise NotImplementedError
while (current = current.next)
count += 1
end
count
end

# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity: ?
# Space Complexity: ?
def has_cycle
raise NotImplementedError
# 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: O(n)
# Space Complexity: O(n)
def get_at_index(index)
Comment on lines +100 to +102

Choose a reason for hiding this comment

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

The space complexity is O(1) because you're not creating a new list.

return nil if @head.nil?

current = @head
index.times do
current = current.next
end
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.

What if the list is shorter than index?

current.data
end

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

Choose a reason for hiding this comment

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

👍

Space and time complexity ❓

return nil if @head.nil?

# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: ?
# Space Complexity: ?
def get_first
raise NotImplementedError
current = @head
print @head.data.to_s + '-->'
while (current = current.next)
print current.data.to_s + '-->'
end
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
def add_last(value)
raise NotImplementedError
end
# method to delete the first node found with specified value
# Time Complexity: O(n)
# Space Complexity: O(n)
def delete(value)
Comment on lines +126 to +128

Choose a reason for hiding this comment

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

👍 , the space complexity is O(1) because you're not creating a new list.

return false if @head.nil?

# 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: ?
def get_last
raise NotImplementedError
current = @head
if current.data == value
@head = current.next
else
until current.next.nil?
if current.next.data == value
current.next = current.next.next
return
end
current.next.next = nil if current.next.next == @tail && current.next.next.data == value
current = current.next
end
end
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity: ?
# Space Complexity: ?
def insert_ascending(value)
raise NotImplementedError
# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
# Time Complexity: o(n)
# Space Complexity: O(n)
# def reverse
# current = @head
# while current != nil
# temp = current.previous
# current.previous = current.next
# current.next = temp
# current = current.previous
# end
# temp = @head
# @head = @tail
# @tail = temp
# end

def reverse
Comment on lines +148 to +163

Choose a reason for hiding this comment

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

👍 , the space complexity is O(1) because you're not creating a new list.

current = @head
previous = nil
while current != nil
temp = current.next # save state
current.next = previous # update the link
previous = current
current = temp
end
@head = previous
end

# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head == nil # don't do anything if the linked list is empty

# navigate to last node
current = @head
while current.next != nil
current = current.next
end
## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def find_middle_value
raise NotImplementedError
# slowPointer = @head
# fastPointer = @head
# if @head !=nil
# while (fastPointer !=nil && fastPointer.next !=nil)
# fastPointer = fastPointer.next.next
# slowPointer = slowPointer.next
# end
# end
# # print("The middle element is: ", slow_ptr.data)
# slowPointer.data
end

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: ?
# Space Complexity: ?
def find_nth_from_end(n)
raise NotImplementedError
end

# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity: ?
# Space Complexity: ?
def has_cycle
raise NotImplementedError
end

# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: ?
# Space Complexity: ?
def get_first
Comment on lines +216 to +218

Choose a reason for hiding this comment

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

👍 , space & time complexity?

@head ? @head.data : nil
end

current.next = @head # make the last node link to first node
# method that inserts a given value as a new last node in the linked list
# Time Complexity: O(n)
# Space Complexity: O(n)
def add_last(value)
Comment on lines +223 to +225

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)

new_node = Node.new(value)
if @head.nil?
add_first(new_node.data)
return
end
current = @head
current = current.next until current.next.nil?
current.next = new_node
new_node.next = nil
end

# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
# Time Complexity: O(n)
# Space Complexity: O(n)
def get_last
Comment on lines +239 to +241

Choose a reason for hiding this comment

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

👍 , the space complexity is O(1) because you're not creating a new list.

return nil if @head.nil?

current = @head
current = current.next until current.next.nil?
current.data
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity: ?
# Space Complexity: ?
def insert_ascending(value)
raise NotImplementedError
end

# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head.nil? # don't do anything if the linked list is empty

# navigate to last node
current = @head
current = current.next until current.next.nil?
current.next = @head # make the last node link to first node
end
end
Binary file added vendor/cache/ansi-1.5.0.gem
Binary file not shown.
Binary file added vendor/cache/builder-3.2.4.gem
Binary file not shown.
Binary file added vendor/cache/coderay-1.1.3.gem
Binary file not shown.
Binary file added vendor/cache/method_source-1.0.0.gem
Binary file not shown.
Binary file added vendor/cache/minitest-5.14.1.gem
Binary file not shown.
Binary file added vendor/cache/minitest-reporters-1.4.2.gem
Binary file not shown.
Binary file added vendor/cache/minitest-skip-0.0.1.gem
Binary file not shown.
Binary file added vendor/cache/minitest-spec-0.0.2.1.gem
Binary file not shown.
Binary file added vendor/cache/pry-0.13.1.gem
Binary file not shown.
Binary file added vendor/cache/rake-13.0.1.gem
Binary file not shown.
Binary file added vendor/cache/ruby-progressbar-1.10.1.gem
Binary file not shown.