INSERT INTO CIRCULAR LINKED LIST | LEETCODE # 708 | PYTHON SOLUTION

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

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

  • @aman4434
    @aman4434 10 หลายเดือนก่อน +7

    I would like to add that your code works, but you may have missed mentioning one case. For something like [3,4,1] and insert 2, our curr reaches 1 and then breaks as curr.next is now 3.
    However we haven't inserted anything yet.
    If we now add the new node in front of curr, it works! As you have done.
    But it is not only the case that we reach outside the loop on a univalued linked list. It is such a case as well. And in this case, it is necessary to add the new value right after curr, and not at any random point in the linked list!
    How I personally deal with this is by:
    curr = head
    flag = 1
    while curr != head or flag == 1:
    flag = 0

    • @ginotozzi7537
      @ginotozzi7537 9 หลายเดือนก่อน

      Good catch! I also noticed this and realized the last bit of logic catches this. But he misses that in the video like you said.

    • @kratiyadav378
      @kratiyadav378 3 หลายเดือนก่อน +1

      This scenario is covered by the last statement before returning the head.
      the curr pointer runs from the head node to the node before head.
      If the new value isn't inserted anywhere in the loop, that means the value has to be inserted between the head and the node before head, which is being done by the last statement. (though he didn't mention this explicitly in the video)

    • @aman4434
      @aman4434 3 หลายเดือนก่อน +1

      @@kratiyadav378 yes I just meant that he missed mentioning it.

    • @yingxu1694
      @yingxu1694 28 วันที่ผ่านมา

      @@kratiyadav378 so actually the last statement is not for linked list with the same value as the first condition already includes it

  • @hzy4455
    @hzy4455 หลายเดือนก่อน

    Thanks for the explanations it helped a lot! I wanted to add that the code works, but you missed to mention one scenario. When we iterated over the linked list, essentially we wanted to find a position between two nodes to insert a value, but the position between the node before head and head was never compared because of the while cur.next!=head condition. The last chunk of code after the while loop actually solved this issue.

  • @tptbythelake
    @tptbythelake หลายเดือนก่อน +1

    I've got Meta onsite next Tuesday - thank you for the excellent videos!

  • @landocodes
    @landocodes 10 หลายเดือนก่อน +2

    I'm not sure if you're comfortable linking to your leetcode, or wanting to go an extra step, but it'd be nice if you could share the actual text of your code. NBD but sometimes the full code doesn't fit on your screen so it'd be a bit helpful.

  • @landocodes
    @landocodes 10 หลายเดือนก่อน +1

    Keep it up bro! I've begun moving from neetcode to this channel. Your solutions/videos are really straight forward.
    Do you plan on doing any system design videos at some point?

  • @manojamrutharaj9071
    @manojamrutharaj9071 2 ปีที่แล้ว

    Awesome explanation covering all the cases. Thanks.
    This problem is somewhat different in which the node starting at head is the least valued node.

  • @patrickcunningham2390
    @patrickcunningham2390 หลายเดือนก่อน

    It seems like case 4 (inserting a value into an array of all the same values) is redundant. If you have a list that is [3, 3, 3] and you are inserting [3], your first iteration would find that 3

  • @YT.Nikolay
    @YT.Nikolay 2 ปีที่แล้ว

    this problem really cracked my head :D Thanks for the explanation!

  • @matthewsaucedo2471
    @matthewsaucedo2471 ปีที่แล้ว

    Awesome explanation!

  • @gokulbalagopal9733
    @gokulbalagopal9733 9 หลายเดือนก่อน

    what if there is a case where we have a circular linked list like this: [3,3,4,] and we want to insert a 9. Shouldn't the 9 be inserted between 4 and the head (3). In such a case, I feel the code explained above may not work. If I have missed something please let me know. Thanks in advance, for your help.

    • @davidmwangi4312
      @davidmwangi4312 6 หลายเดือนก่อน +1

      Its already covered on insert on the edge , case scenario.

  • @subee128
    @subee128 10 หลายเดือนก่อน

    Thank you very much

  • @nithinsastrytellapuri291
    @nithinsastrytellapuri291 10 หลายเดือนก่อน

    Condition on line number 19 is very important

  • @AmolGautam
    @AmolGautam ปีที่แล้ว

    Thank you

  • @Change510
    @Change510 ปีที่แล้ว

    Thanks