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
177 changes: 140 additions & 37 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,98 +18,201 @@ 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)
new_node.next = @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(1)
def search(value)
Comment on lines +31 to 33

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
until current == nil
if current.data == value
return true
else
current = current.next
end
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 +47 to 49

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
return nil
end
max_val = @head.data
current = @head
until current == nil
if current.data > max_val
max_val = current.data
end
current = current.next
end
return max_val
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 +66 to 68

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
return nil
end
min_val = @head.data
current = @head
until current == nil
if current.data < min_val
min_val = current.data
end
current = current.next
end
return min_val
end

####################################################################################

# Additional Exercises
# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: 0(1)
# Space Complexity: 0(1)
def get_first
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
if @head == nil
return nil
end
@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 +98 to 100

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: 0(n)
# Space Complexity: O(1)
def length
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.

👍

raise NotImplementedError
count = 0
current = @head
until current == nil
current = current.next
count += 1
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 +128 to 130

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 > length
return nil
end
current = @head
until current == nil
if count == index
return current.data
else
count += 1
current = current.next
end
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 +148 to 150

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
current = @head
until current.next == nil
puts 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 +159 to 161

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
return nil
end
if @head.data == value
@head = @head.next
end
current = @head
until current.next == nil
if current.next.data == value
current.next = current.next.next
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 +179 to 181

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
previous = nil
current = @head
temp = current.next
# All variables are pointers
until current.next == nil
current.next = previous
previous = current
current = temp
temp = current.next
end
current.next = previous
@head = current
return
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 +199 to 201

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
return nil
else
current = @head
until current.next == nil
current = current.next
end
end
return current.data
end

## Advanced Exercises

###############################################################################################################

## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
Expand Down
18 changes: 9 additions & 9 deletions test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@list.add_first(3)

# Assert
expect(@list.get_at_index(0)).must_equal 3
expect(@list.get_first()).must_equal 3
end

it 'will put the last added item to the front of the list' do
Expand All @@ -32,18 +32,18 @@
@list.add_first(2)

# Assert
expect(@list.get_at_index(0)).must_equal 2
expect(@list.get_first()).must_equal 2

# Act again
@list.add_first(3)

# Assert
expect(@list.get_at_index(0)).must_equal 3
expect(@list.get_first()).must_equal 3
end

it 'will return `nil` for `getFirst` if the list is empty' do

expect(@list.get_at_index(0)).must_be_nil
expect(@list.get_first()).must_be_nil
end
end

Expand All @@ -59,10 +59,10 @@
end

it "returns false if the element is not in the list" do
@list = LinkedList.new
@list.add_first(3)
@list.add_first(2)
expect(@list.search("pasta")).must_equal false
@list = LinkedList.new
@list.add_first(3)
@list.add_first(2)
expect(@list.search("pasta")).must_equal false
end

it "returns false for an empty list" do
Expand Down 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