Also, if you're working with adding and removing values, you could potentially add the same value multiple times, then remove one of them, but still find that you have a node with the same value. maybe that's a feature, maybe that's a bug, depends on how you might use your linked list. another options is to use 'indexes'. so you can insert values in between nodes as well. just some thoughts. There are many ways to implement linked lists :)
And just a few months ago I learn there is an implicit self created if you used an instance method in another method. It makes me reflect on the implicit return created at the end of a method.
Hello, nice videos. Just a small tip :) I believe you could, instead of always calling Node.new(value, nil), assign nextNode = nil in initialize method as default value. Also a question why do you have class in class ??
Sorry but I think there might be a bug in your remove method - if you remove the head when there are more nodes after it, how will you manage to traverse to those nodes? Perhaps I am misunderstanding. Nice video regardless :)
I second that. He has a bug in the remove method. I have implemented a print_list method and it stops due to the return if head.nil? It should be return self.head = node.next_node if node.value.eql?( value ) So first you set node = head than prev_node = nil and than the return and than the until loop....
Also curious, maybe someone already mentioned, but it's common to use @val (instance variables), self.val usually used for class variables. attr_* gives you the methods to get/set instance variables on an object, so in initialize just use @val and @next_node; then you can just say object.val/object.next_node and object.val = 'val', object.next_node = 'val'.
so i tried this and i think there is a bug in your remove method. here is my test: ll = LinkedList.new ll.add(10) ll.add(20) ll.prepend(5) puts ll.head.value #5 puts ll.head.nextNode.value #10 puts ll.head.nextNode.nextNode.value #20 puts ll.find(10) #true puts ll.find(50) #false puts ll.find(20) #true puts ll.find(50) #false puts ll.find(10) #true ll.remove(5) puts ll.head.value #undefined method `value' for nil:NilClass puts ll.head.nextNode.value this is not working for when you remove the first node in the list, it does work if i remove the second or third. but not the first
here is how i solved it: def remove(value) return if head.nil? node = self.head prev_node = nil return self.head = self.head.nextNode if node.value == value while node && value != self.head if node.value == value if prev_node prev_node.nextNode = node.nextNode return else self.head = nil return end end prev_node = node node = node.nextNode end end
thanks to your two videos, i feel comfortable enough with nodes now where as they were a complete mystery before. Keep up the great work, I'll be subscribing.
More videos on ROR and Ruby to come! Please let me know if you have any video suggestions. Thanks for watching!
Create a top level crud api and tricks
Maybe a idea for video 3 how to reverse a linkedList ?
Can you explain what a node is? and what an edge is ? is it like the title of the data structure ? how stuff interacts with each other
More data structure and algorithm videos please
Also, if you're working with adding and removing values, you could potentially add the same value multiple times, then remove one of them, but still find that you have a node with the same value. maybe that's a feature, maybe that's a bug, depends on how you might use your linked list. another options is to use 'indexes'. so you can insert values in between nodes as well. just some thoughts. There are many ways to implement linked lists :)
Awesome suggestions and thoughts Victor! I appreciate your feedback.
And just a few months ago I learn there is an implicit self created if you used an instance method in another method. It makes me reflect on the implicit return created at the end of a method.
next video RoR tricks.
I think in remove method, we need to make self.head = node.nextNode if we are trying to remove first node.
Hello, nice videos. Just a small tip :) I believe you could, instead of always calling Node.new(value, nil), assign nextNode = nil in initialize method as default value. Also a question why do you have class in class ??
Yes, you are right. Thanks!
More DS videos please 🙏
Long time off, but working on more content now!
Prepend could really be a one liner, no need for nil check.
self.head = Node.new value, head
Sorry but I think there might be a bug in your remove method - if you remove the head when there are more nodes after it, how will you manage to traverse to those nodes? Perhaps I am misunderstanding. Nice video regardless :)
I second that. He has a bug in the remove method. I have implemented a print_list method and it stops due to the return if head.nil?
It should be return self.head = node.next_node if node.value.eql?( value )
So first you set node = head than prev_node = nil and than the return and than the until loop....
Yes, you are right. Sorry about this error
Also curious, maybe someone already mentioned, but it's common to use @val (instance variables), self.val usually used for class variables. attr_* gives you the methods to get/set instance variables on an object, so in initialize just use @val and @next_node; then you can just say object.val/object.next_node and object.val = 'val', object.next_node = 'val'.
so i tried this and i think there is a bug in your remove method.
here is my test:
ll = LinkedList.new
ll.add(10)
ll.add(20)
ll.prepend(5)
puts ll.head.value #5
puts ll.head.nextNode.value #10
puts ll.head.nextNode.nextNode.value #20
puts ll.find(10) #true
puts ll.find(50) #false
puts ll.find(20) #true
puts ll.find(50) #false
puts ll.find(10) #true
ll.remove(5)
puts ll.head.value #undefined method `value' for nil:NilClass
puts ll.head.nextNode.value
this is not working for when you remove the first node in the list, it does work if i remove the second or third. but not the first
here is how i solved it:
def remove(value)
return if head.nil?
node = self.head
prev_node = nil
return self.head = self.head.nextNode if node.value == value
while node && value != self.head
if node.value == value
if prev_node
prev_node.nextNode = node.nextNode
return
else
self.head = nil
return
end
end
prev_node = node
node = node.nextNode
end
end
thanks to your two videos, i feel comfortable enough with nodes now where as they were a complete mystery before. Keep up the great work, I'll be subscribing.