Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports - Jillianne #37

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
243 changes: 195 additions & 48 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,132 +18,279 @@ 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
node = Node.new(value, @head)
@head = 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)
raise NotImplementedError
node = @head

until node == nil
if node == value
return true
else
node = node.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:
# Space Complexity
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_max
raise NotImplementedError
return nil if @head == nil

node = @head
max = node.data

until node == nil
if node.data > max
max = node.data
end
node = node.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
raise NotImplementedError
return nil if @head == nil

node = @head
min = node.data

until node == nil
if node.data < min
min = node.data
end
node = node.next
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
raise NotImplementedError
length = 0
node = @head

until node == nil
length += 1
node = node.next
end

return length
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)
raise NotImplementedError
value = nil
node = @head
i = 0

until node == nil
if i == index
value = node.data
break
end
node = node.next
i += 1
end

return value
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
raise NotImplementedError
return nil if @head == nil
node = @head

until node == nil
puts node.data
node = node.next
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)
raise NotImplementedError
node = @head

until node == nil
if node.data == value
@head = node.next
break
elsif node.next.data == value
node.next = node.next.next
break
end
node = node.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
raise NotImplementedError
# 3 (head) -> 2 (tail) -> nil
return nil if !@head

previous = nil
current = @head

until !current

Choose a reason for hiding this comment

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

👍

next_node = current.next # 2, nil
current.next = previous # nil, 3 @head
previous = current # 3 @head, 2
current = next_node # 2, nil
end

@head = previous #2
end


## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity:
# Space Complexity
# Time Complexity: O(2n) -> O(n)
# Space Complexity: O(1)
def find_middle_value
raise NotImplementedError
return nil if !@head

node = @head

length = self.length - 1
length = (length / 2.0).ceil

length.times do
node = node.next
end

return node.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(2n) -> O(n)
# Space Complexity: O(1)
def find_nth_from_end(n)
raise NotImplementedError
length = self.length

Choose a reason for hiding this comment

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

You can actually do this without having to traverse the list twice.

return nil if !(length > n)

node = @head

while n < length - 1
node = node.next
n += 1
end

return node.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:
# Time Complexity:
# Space Complexity
def has_cycle
raise NotImplementedError
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: O(1)
# Space Complexity: O(1)
def get_first
raise NotImplementedError
node = @head

return node == nil ? nil : node.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)
raise NotImplementedError
node = @head

if node == nil
@head = Node.new(value)
else
until node.next == nil
node = node.next
end

node.next = Node.new(value)
end
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
raise NotImplementedError
node = @head

return nil if node == nil

until node.next == nil
node = node.next
end

return node.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
# Time Complexity: O(n)
# Space Complexity: O(1)
def insert_ascending(value)
raise NotImplementedError
node = @head

if node == nil
@head = Node.new(value)
elsif node.data > value
@head = Node.new(value, node)
else
while node.data < value
if node.next != nil
if node.next.data > value
connected_node = node.next
node.next = Node.new(value, connected_node)
break
end
node = node.next
else
node.next = Node.new(value)
break
end
end
end
end

# Helper method for tests
Expand All @@ -160,4 +307,4 @@ def create_cycle

current.next = @head # make the last node link to first node
end
end
end
1 change: 0 additions & 1 deletion test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

describe 'initialize' do
it 'can be created' do

# Assert
expect(@list).must_be_kind_of LinkedList
end
Expand Down