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

Tatiana Linked List #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
198 changes: 163 additions & 35 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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

def initialize(value, next_node = nil)
def initialize(value, next_node=nil)
@data = value
@next = next_node
end
Expand All @@ -18,73 +18,156 @@ 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
# Space Complexity O
def add_first(value)
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
def search(value)
raise NotImplementedError
current = @head

while current
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:
# Space Complexity
# Time Complexity: O(n)
# Space Complexity: O
def find_max
raise NotImplementedError
visited_node = @head
return nil if visited_node == nil

max = visited_node.data

while visited_node != nil
if visited_node.data > max
max = visited_node.data
end
visited_node = visited_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
def find_min
raise NotImplementedError
return nil if @head == nil

visited_node = @head
min = visited_node.data

while visited_node != nil
if visited_node.data < min
min = visited_node.data
end
visited_node = visited_node.next
end
return min
end


# method that returns the length of the singly linked list
# Time Complexity:
# Space Complexity
# Time Complexity: O(n) - must count all nodes
# Space Complexity: O - constant value returned
def length
raise NotImplementedError
count = 0
current = @head

if @head == nil
return 0
else
until current == nil
current = current.next
count += 1
end
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) - worst case
# Space Complexity: O - constant value returned
def get_at_index(index)
raise NotImplementedError
current = @head

return nil if @head == nil
count = 0

while count < index && current.next != nil
current = current.next
count += 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 - just printing to console, no new memory needed
def visit
raise NotImplementedError
current = @head

while current.next != nil
puts @current.data
current = current.next
end
# for last node
puts current.data
end

# method to delete the first node found with specified value
# Time Complexity:
# Space Complexity
# Time Complexity: O(n)
# Space Complexity O
def delete(value)
raise NotImplementedError
current = @head

if @head == nil
return 0
end

# if head node has the value to delete
if current.data == value
@head = current.next
end

while current!= nil
if current.next != nil && 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(n)
def reverse
raise NotImplementedError
current = @head
reversed_list = LinkedList.new

while current.next != nil
reversed_list.add_last(current)

Choose a reason for hiding this comment

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

This isn't working because it is adding the node to the new list, rather than the node's data. It's also adding them all to the last, rather than the front.

Lastly this loop is skipping the last node in the list (check the while condition).

current = current.next
end
return reversed_list
end


Expand All @@ -98,10 +181,33 @@ def find_middle_value

# 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

Choose a reason for hiding this comment

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

Space complexity O(1)

def find_nth_from_end(n)
raise NotImplementedError
current = @head

return nil if @head == nil

# need to know length
length = 0
while current != nil
current = current.next
length += 1
end

# return position from the end
position = length - n - 1
return nil if position < 0

search_node = @head
starting_index = 0
while search_node != nil
if starting_index == position
return search_node.data
end
starting_index += 1
search_node = search_node.next
end
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -120,22 +226,44 @@ def has_cycle
# Time Complexity:
# Space Complexity
def get_first
raise NotImplementedError
return @head if @head.nil?

first_node = @head
return first_node.data
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity:
# Time Complexity:O(n)
# Space Complexity
def add_last(value)
raise NotImplementedError
last_node = Node.new(value)
current = @head

if @head == nil
@head = last_node
else
while current.next != nil
current = current.next
end
current.next = last_node
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
def get_last
raise NotImplementedError
count = 0
current = @head
if !current
return nil
end

while current.next != nil
current = current.next
end
return current.data
end

# method to insert a new node with specific data value, assuming the linked
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 @@ -168,7 +168,7 @@
@list.add_first(2)

# Act
# delete fist node (requires updating head)
# delete first node (requires updating head)
@list.delete(2)

# Assert
Expand All @@ -188,7 +188,7 @@
expect(@list.find_max).must_equal 9
expect(@list.find_min).must_equal 3

# delete fist node (requires updating head)
# delete first node (requires updating head)
@list.delete(4)
expect(@list.get_first).must_equal 3
expect(@list.length).must_equal 2
Expand Down