Skip to content

Instantly share code, notes, and snippets.

@roolo
Last active September 18, 2018 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roolo/c945e7b76aad616e5a31679e1b92dd6f to your computer and use it in GitHub Desktop.
Save roolo/c945e7b76aad616e5a31679e1b92dd6f to your computer and use it in GitHub Desktop.
Removing node from a list
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