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
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)
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.
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.
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?
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
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.
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
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.
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)
@@kratiyadav378 yes I just meant that he missed mentioning it.
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.
I've got Meta onsite next Tuesday - thank you for the excellent videos!
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.
Awesome explanation covering all the cases. Thanks.
This problem is somewhat different in which the node starting at head is the least valued node.
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?
this problem really cracked my head :D Thanks for the explanation!
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
Awesome explanation!
Thank you very much
Condition on line number 19 is very important
Thank you
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.
Its already covered on insert on the edge , case scenario.
Thanks