INSERT INTO CIRCULAR LINKED LIST | LEETCODE # 708 | PYTHON SOLUTION
Vložit
- čas přidán 24. 07. 2024
- In this video we are solving a popular Facebook interview question: Insert Into Sorted Linked List (Leetcode # 708).
The question isn't really that difficult but there are four cases you need to identify and handle in order to fully solve this question. Let's go through them and figure out how to code this.
TIMESTAMPS:
00:00 Intro
00:15 Question Prompt
01:05 Basic Example
02:11 Solution Intuition
07:25 Coding
13:30 Time/Space Complexity
15:00 Outro - Věda a technologie
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.
Awesome explanation covering all the cases. Thanks.
This problem is somewhat different in which the node starting at head is the least valued node.
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.
this problem really cracked my head :D Thanks for the explanation!
Awesome explanation!
Thank you very much
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?
Thank you
Thanks
Condition on line number 19 is very important
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.