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

Hana #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Hana #21

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
218 changes: 166 additions & 52 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pry'

# Defines a node in the singly linked list
class Node
Expand All @@ -14,79 +15,213 @@ def initialize(value, next_node = nil)
class LinkedList
def initialize
@head = nil # keep the head private. Not accessible outside this class
@tail = nil

Choose a reason for hiding this comment

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

Nice!

end

# 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: O(1) constant - because you are only redirecting the head to the new node.
# Space Complexity:
def add_first(value)
new_node = Node.new(value, @head)

if !@head
@tail = new_node
end
@head = new_node
# current = @head
# while current.next
# current = current.next
# end
# @tail = current

print @head.data

Choose a reason for hiding this comment

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

This doesn't need to be in the submission.

end

# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity:
# Space Complexity
def add_first(value)
raise NotImplementedError
def get_first
if @head
return @head.data
end
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# method that returns the length of the singly linked list
# Time Complexity:
# Space Complexity
def search(value)
raise NotImplementedError
def length
current = @head
length = 0

while current
length += 1
current = current.next
end

return length
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity:
# Space Complexity
def add_last(value)
new_node = Node.new(value)
@tail = new_node

Choose a reason for hiding this comment

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

You should do @tail.next = new_node before this. Then except for checking if the list was empty before, you would be finished. No loop required.


if !@head
@head = new_node
else
current = @head
while current.next
current = current.next
end
current.next = @tail
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
if !@head
return nil
end
return @tail.data
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
def get_at_index(index)
current = @head

if current
index.times do

Choose a reason for hiding this comment

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

What if index is larger than the size of the list?

current = current.next
end
return current.data
else
return nil
end
end

# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
def find_max
raise NotImplementedError
if @head
current = @head
max = 0

Choose a reason for hiding this comment

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

What if the list is filled with negative numbers? What if it's not a list of integers?

while current
current.data > max ? max = current.data : current = current.next
end
return max
else
return nil
end
end

# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
def find_min
raise NotImplementedError
if @head
current = @head
min = @head.data
while current
current.data < min ? min = current.data : current = current.next
end
return min
else
return nil
end
end


# method that returns the length of the singly linked list
# method to delete the first node found with specified value
# Time Complexity:
# Space Complexity
def length
raise NotImplementedError
# CHRIS - this isn't working! I'm pretty sure this is because it's not updating tail, bu

Choose a reason for hiding this comment

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

This is because your add_last method isn't updating @tail properly.

def delete(value)
if @head == nil
return
end
current = @head

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

while current.next
if current.next.data == value
if current.next == @tail
@tail = current
current.next = nil
return
else
current.next = current.next.next
return
end
end
current = current.next
end
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
# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity:
# Space Complexity
def get_at_index(index)
raise NotImplementedError
def search(value)
current = @head

Choose a reason for hiding this comment

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

What if @head is nil?


while current
if current.data == value
return true
end
current = current.next
end
return false
end

# method to print all the values in the linked list
# Time Complexity:
# Space Complexity
def visit
raise NotImplementedError
end
return if !@head

current = @head
while current
puts "#{current.data}"
current = current.next
end

# method to delete the first node found with specified value
# Time Complexity:
# Space Complexity
def delete(value)
raise NotImplementedError
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
def reverse
raise NotImplementedError
end
return if !@head || [email protected]

count = self.length
current = @head
index = count - 1

count.times do

Choose a reason for hiding this comment

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

This works, but it's an O(n2) algorithm. Is there a better way?

temp = self.get_at_index(index)
self.delete(temp)
self.add_first(temp)
end
end

## Advanced Exercises
# returns the value at the middle element in the singly linked list
Expand All @@ -101,7 +236,11 @@ def find_middle_value
# Time Complexity:
# Space Complexity
def find_nth_from_end(n)
raise NotImplementedError
return if !@head
return @head.data if n == 0
return @tail.data if n == 1

binding.pry
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -113,31 +252,6 @@ def has_cycle
raise NotImplementedError
end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity:
# Space Complexity
def get_first
raise NotImplementedError
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity:
# Space Complexity
def add_last(value)
raise NotImplementedError
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
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity:
Expand Down