Data Structures in Python: Circular Linked Lists -- Remove Node

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ต.ค. 2024

ความคิดเห็น • 41

  • @mrinmayigavali2049
    @mrinmayigavali2049 5 ปีที่แล้ว +12

    Awesome tutorial as always!
    I added one more condition at the end as I was not able to remove the key if the circular linked list contained only one node.
    Hope this is right.
    if self.head==self.head.next and self.head.data==key:
    self.head=None

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      Hmm, I thought this case was properly dealt with, but perhaps I missed it. Thanks for the catch, and appreciate you watching and commenting! Cheers :)

    • @MineCarter1
      @MineCarter1 4 ปีที่แล้ว +1

      After doing this you also have to add:
      if self.head is None:
      return
      at beginning of the method.

  • @lunghunglin5729
    @lunghunglin5729 4 ปีที่แล้ว +1

    def remove(self,key):
    if self.head.data == key:
    cur = self.head
    while cur.next != self.head:
    cur = cur.next
    cur.next = self.head.next
    self.head = self.head.next
    else:
    cur = self.head
    prev = None
    while cur.data != key:
    prev = cur
    cur = cur.next
    prev.next = cur.next
    I used a similar method but did a little bit change in the while loop in the else statement. I think it works!
    As always, thank you for the tutorial and keep on the great work!!

  • @euwofbw
    @euwofbw 4 ปีที่แล้ว +1

    Hi Vincent ! thanks for the great tutorial!
    3 issues
    1, cur=cur.next is inefficient, if key is at tail, the while loop will end up running *twice* before it can exit out. I suggest use break
    2, throw error when it's an empty list which can be easily modified by adding a if statement
    3, cant remove when there is only one node, someone else in the comment section already pointed it out and gave great solution
    thanks again for your work!

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      I think the clarity of less code for the one edge case of just an extra iteration is probably worth it. Thanks for pointing that out, and thanks for watching!

  • @prabhanjantrivedi
    @prabhanjantrivedi 4 ปีที่แล้ว

    Salute to you. You are really awesome.
    Just one thing i wanted to point in above code.
    If CL is A>B>C>C>D, after removing C, it will result A>B>C>D, but if CL is A>B>C>D>C, after removing C, it will give A>B>D

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Thanks for the comment, and thanks for watching!

  • @ouroboros7388
    @ouroboros7388 2 ปีที่แล้ว +2

    This playlist is amazing, thank you for your amazing work!!

  • @ian_senior
    @ian_senior 4 ปีที่แล้ว

    i am really learning a lot from all of your data structure playlist videos. i implemented my own version of the remove method. seems to work fine for all the edge cases.
    def remove(self, data):
    head = node = prev = self.head
    while node.next != head:
    if node.data == data and node != head: break
    prev, node = node, node.next
    if self.head.data == data:
    self.head = head.next
    node.next = self.head
    if node.data == data: prev.next = node.next

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Awesome good to hear, and thanks for sharing!

  • @iplmaniac
    @iplmaniac 5 ปีที่แล้ว +3

    Great video.Why is there a need for " cur = cur.next" in the last line of the function?The program seems to run without it

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว +1

      Thank you, Sarthak! My recommendation to you would be to try to take it out and see what happens for some edge cases. Go ahead and also pepper in some print statements to see what occurs and see if you can figure it out from that. Let me know if not. Cheers!

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @Daniel Kua What are "all the edge cases" in this instance?

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @Daniel Kua Hahaha, so you're saying you tried nothing when you tried all the edge cases?

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @Daniel Kua Google. It's a wonderful thing.

  • @tomio444
    @tomio444 5 ปีที่แล้ว +2

    Great video and the visual representation in the previous one really helped. I still do have one question; how do we delete the last node in a list? I get that this would be the case if the next-pointer of a node would point to itself, but how do we let the __repr__ or print function know? Is it enough if we set the data of that last node to None?

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      Thank you. In order to delete the last node, you would check if the next node is head. If the next node is head, you know you're at the last node. Does that make sense?

    • @tomio444
      @tomio444 5 ปีที่แล้ว +1

      @@LucidProgramming yes it does! Checking if the next node is head and then setting not it's data field but the node itself to None worked for me

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      @@tomio444 Great. Cheers for watching and thanks for letting me know!

  • @shubhamjaiswal2322
    @shubhamjaiswal2322 5 ปีที่แล้ว

    Hello, @LucidProgramming I have a question regarding the deletion of the node like you wrote
    while curr.next != self.head:
    prev = curr
    curr = curr.next #why did you move the next pointer here
    if curr.data is data:
    prev.next = curr.next
    curr = curr.next
    #why not here, like we used to do in earlier videos
    please explain I am bit confused.
    Thank you
    [UPDATE]
    * We can write the above program like this also. This also works flawlessly.*
    else:
    curr = self.head
    prev = None
    while curr.next != self.head:
    nxt = curr.next
    if curr.data is data:
    prev.next = nxt
    prev = curr
    curr = nxt
    Thank you

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      With the update, did you answer your own question? I can't quite tell if you did or not. Cheers, and thanks again for your comments!

    • @shubhamjaiswal2322
      @shubhamjaiswal2322 5 ปีที่แล้ว

      @@LucidProgramming With my update i am telling that we can write the program in that way also.
      Yes, I got the answer of my question by myself. It was the way of writing the program, I wrote slightly different from yours.
      Thank you

  • @ian_senior
    @ian_senior 4 ปีที่แล้ว +1

    the github link in the video description points to the split list repo. here is the remove repo if anyone is looking for it.
    github.com/vprusso/youtube_tutorials/blob/master/data_structures/linked_list/circular_linked_list/circular_linked_list_remove.py

  • @Dev-dk3ez
    @Dev-dk3ez 5 ปีที่แล้ว

    For else condition, the more efficient way is to check the key in while loop. This would ensure less iteration unless you want to delete the last node. Let me know if you think otherwise.

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      Hi Dev. I'm not sure I agree with your assessment. Your approach would involve adding an unnecessary check on every iteration of the loop.

  • @ybruce61414
    @ybruce61414 6 ปีที่แล้ว

    Hey so appreciate your detailed tutorial !
    I wanna ask if it would be an easy way to write:
    else:
    cur = self.head
    prev = None
    While cur.data != key:
    prev = cur
    cur = cur.next
    prev.next = cur.next

    • @LucidProgramming
      @LucidProgramming  6 ปีที่แล้ว +3

      Hello! Thanks for watching and for the comment. If you run through your suggestion, I think you'll encounter an error if the element you're looking for is not present in the list. Basically, you keep looking while the data is not equal to the key. Well, since this is a circular linked list and say the element is not in the list, you'll just keep looping. Did you try to make these changes yourself? I assume this is what you would have seen if you did so.

    • @ybruce61414
      @ybruce61414 6 ปีที่แล้ว

      LucidProgramming
      so grateful for your patient reply and instruction! You're right, I just ignore the scenario that if the element is not in the list. I only consider the case that all the element I wanna remove! Thanks again for your reply :)

    • @LucidProgramming
      @LucidProgramming  6 ปีที่แล้ว

      No worries, that's an easy case to miss. Thanks for watching, and happy coding!

  • @sutingyang9439
    @sutingyang9439 5 ปีที่แล้ว

    Hey, I am the one who asked why self.head = self.head.next is a must. I have made comment under another video, sorry for that!
    Could you please explain line 91 for me ?

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      I would suggest you try taking the line out and trying some edge cases to see why this is there.

  • @arjunreddy3615
    @arjunreddy3615 4 ปีที่แล้ว

    Could we just break out of the loop once we have found the key and altered the next position of the previous nose as below...
    As it is not required to go through the list again since we aimed at removing a single node..
    code:
    def remove(self,key):
    if self.head.data == key:
    cur = self.head
    while cur.next != self.head:
    cur = cur.next
    cur.next = self.head.next
    self.head = self.head.next
    else:
    prev = None
    cur = self.head
    while cur.next != self.head:
    prev = cur
    cur = cur.next
    if cur.data == key:
    prev.next = cur.next
    #cur = cur.next
    break

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Sure, that would save you some potential checks.

  • @jamesjanes7936
    @jamesjanes7936 4 ปีที่แล้ว

    does this work if head is the only node and its pointing to itself?

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Have you tried that case?

    • @jamesjanes7936
      @jamesjanes7936 4 ปีที่แล้ว

      LucidProgramming I wouldn’t have asked if I did, but thanks for the answer that was really helpful

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @@jamesjanes7936 Cheers, and thanks for watching!

  • @biswaranjanbiswal1806
    @biswaranjanbiswal1806 4 ปีที่แล้ว

    Last node remove isn't working

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Seems to work for me. Are you sure you're using the code on the GitHub?