programming-examples/ruby/Algorithms/delete_kth_node.rb
2019-11-15 12:59:38 +01:00

22 lines
367 B
Ruby

def delete_kth_node(head, k)
return false unless head
current = head
count = 1
while current.next
current = current.next
count += 1
end
return false if count - k < 0
current = head
(0...count - k).each do
current = current.next
end
current.value = current.next.value || nil
current.next = current.next.next || nil
head
end