Skip to content
Open
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
186 changes: 171 additions & 15 deletions linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ 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)
def initialize(value, next_node=nil)
@data = value
@next = next
@next = next_node
end
end

Expand All @@ -19,77 +19,232 @@ 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
def insert(value)
puts "Not implemented"
# puts "Not implemented"
# id node that new node should point to
if @head != nil
current = @head
@head = Node.new(value)
@head.next = current
else
@head = Node.new(value)
@head.next = nil
end
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)
puts "Not implemented"
current = @head

until current.next == nil
return true if current.value == value
end
return false
end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max
puts "Not implemented"
current = @head
max = current.data

while current
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
def find_min
puts "Not implemented"
current = @head
min = current.data

while current.next != nil
min = current.data if current.data < min
current = current.next
end

return min
end

# method that returns the length of the singly linked list
def length
puts "Not implemented"
length = 0
current = @head

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

return length
end

# method to return the value of the nth element from the beginning
# assume indexing starts at 0 while counting to n
def find_nth_from_beginning(n)
puts "Not implemented"
current = @head

n.times do
current = current.next
end

return current.data
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
def insert_ascending(value)
puts "Not implemented"
current = @head
previous = nil
last = nil

until value < current.data
previous = current
current.next ? current = current.next : break
end

insert_node = Node.new(value)

if !current.next && value > current.data
current.next = insert_node
else
insert_node.next = current
previous.next = insert_node
end
end

# method to print all the values in the linked list
def visit
puts "Not implemented"
current = @head
while current
p current.data
current = current.next
end
end

# method to delete the first node found with specified value
def delete(value)
puts "Not implemented"
return if @head == nil

current = @head
@head = current.next if current.data == value
previous = nil

while current.next
if current.data == value
current = current.next
previous.next = current if previous
break
end
previous = current
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 complex - constant - cause visiting each node once so depends on linklist size
def reverse
puts "Not implemented"
current = @head
previous = nil
temp = current.next

while current
temp = current.next
current.next = previous
previous = current
current = temp
end

@head = previous
end

## Advanced Exercises
# returns the value at the middle element in the singly linked list

# check input - head is nil return
# if head nex is nil return value at head

# set slow head
# set fast head next

# algo steps
# while fast is not nil
# update slow next
# update falst next
## if fast next is not nil - update fast next

# return value at slow
def find_middle_value
puts "Not implemented"
return if @head == nil
return @head.data if @head.next == nil

slow = @head
fast = @head.next

while fast.next
slow = slow.next
fast = fast.next
fast = fast.next if fast.next
end

return slow.data
end

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
def find_nth_from_end(n)
puts "Not implemented"
return if @head == nil

current = @head
trailing = nil

i = 0

until i == n
i += 1
current = current.next
end

trailing = @head

while current.next
current = current.next
trailing = trailing.next
end

return trailing.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.
def has_cycle
puts "Not implemented"
# have a fast and node - when they reach the same position you have a cycle

# two pointers/references, fast and slow, slown and fast start at head
# fast moves twice as fast
# if fast becomes nil - no cycle
# if fast and slow same node - then true cycle
return false if !@head || !@head.next

slow = @head
fast = @head

while fast
slow = slow.next
fast = fast.next

fast = fast.next if fast

return true if fast == slow
end

return false
end

# Creates a cycle in the linked list for testing purposes
Expand All @@ -109,6 +264,7 @@ def create_cycle

## --- END of class definitions --- ##


# Create an object of linked list class
my_linked_list = LinkedList.new()

Expand Down