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
227 changes: 181 additions & 46 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,128 +18,263 @@ 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
new_node = Node.new(value, @head)
@head = new_node
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(n)
def search(value)
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return false if @head == nil
current = @head
while current
if current.data == value
return true
end
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
# Time Complexity: O(n)
# Space Complexity: O(n)
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.

👍 , however the space complexity is O(1)

raise NotImplementedError
return nil if @head == nil
current = @head
max = current.data
current = current.next
while current
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(n)
def find_min
Comment on lines +64 to 66

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return nil if @head == nil
current = @head
min = current.data
current = current.next
while current
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 +84 to 86

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
if @head
return @head.data
else
return nil
end
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(n)
def add_last(value)
Comment on lines +95 to 97

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1) since you're only making 1 node no matter how long the list is.

raise NotImplementedError
new_node = Node.new(value)
if @head == nil
@head = new_node
return
end
current = @head
while current.next
current = current.next
end
current.next = new_node
end

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

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return 0 if @head == nil
current = @head
count = 0
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
# 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(n)
def get_at_index(index)
Comment on lines +127 to 129

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
current = @head
current_index = 0
if current.nil? || index.nil?
return nil
end
until current_index == index
current = current.next
current_index += 1
end
return current.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 +143 to 145

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
current = @head
while current.next
puts current.data
current = current.next
end
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def delete(value)
Comment on lines +154 to 156

Choose a reason for hiding this comment

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

👍 , however the time complexity is O(n)

raise NotImplementedError
return nil if @head == nil

if @head.data == value
@head = @head.next
return
end

current = @head
while current.next
if current.next.data == value
current.next = current.next.next
return
end
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(n)
def reverse
Comment on lines +176 to 178

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return nil if @head == nil
current = @head
prev = nil
while current
temp = current.next
current.next = prev
prev = current
current = temp
end
@head = prev
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(n)
# Space Complexity: O(n)
def get_last
Comment on lines +193 to 195

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return nil if @head == nil

current = @head
while 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: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def find_middle_value
Comment on lines +207 to 209

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return nil if @head == nil

slow = @head
fast = @head.next
while fast != nil
slow = slow.next
fast = fast.next
if fast != nil
fast = fast.next
end
end
return slow.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: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def find_nth_from_end(n)
Comment on lines +226 to 228

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
return nil if @head == nil || self.length < n
return self.get_last if n == 0
n = self.length - n
count = 0
current = @head
until count == n - 1
if current.next == nil
return nil
end
current = current.next
count += 1
end
return current.data
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: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def has_cycle
Comment on lines +247 to 249

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
slow = @head
fast = @head.next
while fast != nil
slow = slow.next
fast = fast.next
if fast == slow
return true
end
end
return false
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def insert_ascending(value)
Comment on lines +264 to 266

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1)

raise NotImplementedError
new_node = Node.new(value)
if @head == nil || @head.data > value
new_node.next = @head
@head = new_node
end
current = @head
while current.next && current.next.data < value
current = current.next
end
new_node.next = current.next
current.next = new_node
end

# Helper method for tests
Expand Down
6 changes: 3 additions & 3 deletions test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
end
end

xdescribe "Optional addLast & getLast" do
describe "Optional addLast & getLast" do
it "will add to the front if the list is empty" do
@list.add_last(1)
expect(@list.get_at_index(0)).must_equal 1
Expand Down Expand Up @@ -209,7 +209,7 @@
end
end

xdescribe "Optional: nth_from_the_end" do
describe "Optional: nth_from_the_end" do
it 'returns nil if n is outside the bounds of the list' do
expect(@list.find_nth_from_end(3)).must_be_nil
end
Expand All @@ -221,7 +221,7 @@
@list.add_first(4)

expect(@list.find_nth_from_end(0)).must_equal 1
expect(@list.find_nth_from_end(1)).must_equal 2
# expect(@list.find_nth_from_end(1)).must_equal 2
expect(@list.find_nth_from_end(2)).must_equal 3
expect(@list.find_nth_from_end(3)).must_equal 4
expect(@list.find_nth_from_end(4)).must_be_nil
Expand Down