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
231 changes: 185 additions & 46 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,128 +18,267 @@ 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)
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
current = @head
while current != nil
return true if current.data == value

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(1)
def find_max
Comment on lines +44 to 46

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?

max = @head.data
current = @head
while current != nil
max = current.data if current.data > max
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 +61 to 63

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?

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @head.nil? ? nil : @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 +87 to 89

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
until current.next.nil?
current = current.next
end

current.next = Node.new(value)
end
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 +103 to 105

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
count = 0
current = @head
while 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(1)
def get_at_index(index)
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
return nil if @head.nil?

current = @head
index.times do
return nil if current.nil?

current = current.next
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(1)
def visit
Comment on lines +135 to 137

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
while current != nil
print current.data
end
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)
Comment on lines +147 to 149

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return if @head.nil?

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

current = @head
until current.next.nil?
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(1)
def reverse
Comment on lines +169 to 171

Choose a reason for hiding this comment

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

👍

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

current = @head
previous = nil
until current.nil?
temp = current.next
current.next = previous
previous = current
current = temp
end

@head = previous
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(1)
def get_last
Comment on lines +188 to 190

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: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_middle_value
Comment on lines +203 to 205

Choose a reason for hiding this comment

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

👍, good use of a fast and slow reference.

raise NotImplementedError
if @head.nil?
return nil
else
slow = @head
fast = @head.next
while fast != nil
slow = slow.next
fast = fast.next
fast = fast.next if fast != nil
end

return slow
end
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(1)
def find_nth_from_end(n)
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.

👍 I like the use of the trailing reference.

raise NotImplementedError
return nil if @head.nil?

current = @head
n.times do
current = current.next
return nil if current.nil?
end

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

return trailing_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(1)
def has_cycle
Comment on lines +246 to 248

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 false
end

slow = @head
fast = @head
while fast != nil
slow = slow.next
fast = fast.next
fast = fast.next if fast != nil

return true if fast == slow
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(1)
def insert_ascending(value)
Comment on lines +268 to 270

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)
return
end

current = @head
while current.next != nil && current.next.data > value
current = current.next
end

current.next = Node.new(value, current.next)
end

# Helper method for tests
Expand Down
4 changes: 2 additions & 2 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 Down