Last active
September 18, 2018 13:12
-
-
Save roolo/c945e7b76aad616e5a31679e1b92dd6f to your computer and use it in GitHub Desktop.
Removing node from a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumberListNode | |
attr_accessor :child, :value | |
def initialize child:, value: | |
@child = child | |
@value = value | |
end | |
end | |
def remove_node head:, unwanted_node: | |
return head.child if head.value == unwanted_node | |
return if head.child.nil? | |
puts head.value | |
if head.child.value == unwanted_node | |
head.child = head.child.child | |
else | |
remove_node head: head.child, unwanted_node: unwanted_node | |
end | |
end | |
list = NumberListNode.new( | |
child: NumberListNode.new( | |
child: NumberListNode.new( | |
child: nil, | |
value: 3 | |
), | |
value: 2 | |
), | |
value: 1 | |
) | |
remove_node head: list, unwanted_node: 5 | |
require 'pp' | |
pp list.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment