Remove Duplicates from Sorted List - Leetcode 83 - Python

  Рет қаралды 63,426

NeetCode

NeetCode

Күн бұрын

Пікірлер: 54
@trafalgarlaw98
@trafalgarlaw98 3 жыл бұрын
hurts to see content like this doesn't get enough support
@sherwan-yt
@sherwan-yt Жыл бұрын
Your videos literally show that programming is not just about coding. Thank you.
@darko8376
@darko8376 3 жыл бұрын
I am here to show my support, you are really talented at giving a clear thinking process to help people divide and conquer questions that might seem intimidating at the first glance. Good joooob!!
@SelfLoveIsKey
@SelfLoveIsKey Жыл бұрын
You did a great job breaking this problem down into digestible pieces. Thank you 🙏🏾
@shakhzod_shermatov
@shakhzod_shermatov 2 ай бұрын
I just couldn't leave this page without expressing my gratitude for perfect explanation. Thank you so much, you're a lifesaver.
@priya6047
@priya6047 3 жыл бұрын
this was super helpful but can't you eliminate the need for two loops by doing something like this instead? if(head == null){ return null; } ListNode first = head; while(first.next != null){ if(first.next.val != first.val){ first = first.next; } else{ first.next = first.next.next; } } return head;
@MahmoudHassan-mm6gz
@MahmoudHassan-mm6gz 2 жыл бұрын
Why would you want to avoid the two loops? it is still O(n) time and O(1) space!
@nicholasvane4249
@nicholasvane4249 2 жыл бұрын
@@MahmoudHassan-mm6gz It seems more intuitive to me if you only have one loop. Tbh i'm a bit confused why the solution he gave us used two loops. If you only need to loop through each element once, why use two loops for that?
@hamoodhabibi7026
@hamoodhabibi7026 2 жыл бұрын
Technically it's still one loop, so if i rewrite it like this it's still the same code :) cur = head while cur and cur.next: if cur.val == cur.next.val: cur.next = cur.next.next else: cur = cur.next return head
@jackklive1234
@jackklive1234 3 жыл бұрын
Thank you for the awesome video, just curious if we set the cur node to head initially and iterate through the linked list (deleting repeated nodes as necessary) using cur but return head, how do we know that head has the updated nodes since we performed the deletions on cur and not head? Shouldn't the "return head" command simply return the original linked list? If anyone knows please enlighten me. Thanks in advance
@eraivansaran5836
@eraivansaran5836 3 жыл бұрын
In python assigning one variable to another variable creates an Alias of each variable. An alias is a variable that points to the same object in memory as another variable. Hope that helps
@jackklive1234
@jackklive1234 3 жыл бұрын
@@eraivansaran5836 Thank you for the clarification, it completely slipped my mind that the cur variable still points to the same memory as the original head object
@ngochainguyen91
@ngochainguyen91 2 жыл бұрын
Same question 😆😂
@tempshare1804
@tempshare1804 3 жыл бұрын
I am starting to think right, thanks to you
@ponceedi000
@ponceedi000 2 жыл бұрын
Nested while loops work just fine but I prefer just one: current = head while current.next: if current.val == current.next.val: current.next = current.next.next else: current = current.next return head
@abdifatahmoh
@abdifatahmoh 2 жыл бұрын
while current.next condition itself doesn't work, you need to say while current and current.next:
@vivekanpm3130
@vivekanpm3130 2 жыл бұрын
@@abdifatahmoh Why we have specify both ?
@sabu4539
@sabu4539 2 жыл бұрын
@@vivekanpm3130 cuz we using current and current.next, for current.val == current.next.val
@andreytamelo1183
@andreytamelo1183 2 жыл бұрын
With all due respect, imo, this code is a bit simpler to read: if (head is null) return null; ListNode back = head; ListNode front = head.next; while (front is not null) { if (front.val != back.val) back = back.next; front = front.next; back.next = front; } return head;
@metaverse413
@metaverse413 Жыл бұрын
OMG finally someone commented on this I solved it exactly the same way you did and it works fine
@manjinderrandhawa5094
@manjinderrandhawa5094 2 жыл бұрын
How about this approach .. def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: itr = head if itr is None: return itr while itr.next: if itr.val == itr.next.val: itr.next = itr.next.next else: itr = itr.next return head
@nikhilbhardwaj6055
@nikhilbhardwaj6055 19 күн бұрын
yes my code is exactly same as you, but his code is faster than that I wonder how
@ngochainguyen91
@ngochainguyen91 2 жыл бұрын
Can you clear why while loop didn’t change anything on head. But head had changed???
@bruce716
@bruce716 2 жыл бұрын
In worst case, if the Sorted List has no duplicates, could we assume the big O is O(n^2) instead of O(n)? Because head node will be through all nodes which is O(n) at least. The big O should definitely higher than O(n)? However, we know it would not be more than O(n^2) for sure. Please let me know if my understanding is on the right track.
@shaguntripathi2554
@shaguntripathi2554 Жыл бұрын
I was about to comment this same! And Yes I guess, in the worst case when there's no duplicate, the TC will me O(n^2) and in good case the TC will lie between (n,n^2) with round brackets, since round brackets doesn't include sensitive/extreme points( ie n and n^2)
@Famelhaut
@Famelhaut Жыл бұрын
If there is no duplicate then the inner while loop won't run. For the inner while loop to run it needs for current to have a next node AND that it's next value is equal to the current value. Think of the inner while loop as an IF statement that triggers for every node. You can actually replace the while loop for an IF ELSE statement and the code will run fine.
@derekstackboy6583
@derekstackboy6583 9 ай бұрын
what blackboard you are using?
@varunshrivastava2706
@varunshrivastava2706 3 жыл бұрын
@NeetCode Please create your own dsa with python course. It will be off the charts there isn't many resources for dsa with python on KZbin. Please man...
@Septix
@Septix 3 жыл бұрын
If you want a head start there’s a book that gets close and the explanations are great. It’s called Computer Science Distilled it’s very beginner friendly but the code is pseudo due which honestly looks just like python and can help give you an idea on general dsa concepts. If neet does a full course I’d honestly pay for it tho I love this guy
@varunshrivastava2706
@varunshrivastava2706 3 жыл бұрын
@@Septix yeah thanks for the suggestion although I have the basic knowledge of all data structures but wanted videos on dp, and some graph algos from this channel. Since these topics really important.
@aynuayex
@aynuayex Жыл бұрын
your solution is super useful, but here is my solution using only one loop class Solution: def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: return head prev = head curr = head.next while curr: if prev.val == curr.val: temp_next = curr.next curr.next = None prev.next = temp_next curr = temp_next else: prev = curr curr = curr.next return head
@rarnald1089
@rarnald1089 3 жыл бұрын
Can you explain why u are using dummy node 🙄.you used dummy node in merging 2 sorted list.
@MahmoudHassan-mm6gz
@MahmoudHassan-mm6gz 2 жыл бұрын
Dummy node is good if you may want to change or delete the head node... Instead of treating these cases as edge cases and have many ifs and elses in your code, you can create a dummy node before head, now head is just another normal node... and at the end you return dummyNode.next (the new head)
@IMDash_freak002
@IMDash_freak002 2 ай бұрын
i am noobie in linked list and also for coding i have question i put same condition second part of second loop if if statement but it says time exceeded why??
@PETERPAN-b1f
@PETERPAN-b1f 2 жыл бұрын
I have a question... In the code , we didnt change the 'head', why it will be changed and be the final answer at the end? Anyone?
@anishkhatiwada2502
@anishkhatiwada2502 2 жыл бұрын
as cur is set(=) to head, they are also pointing to the same physical address of a node.. Think like, we have an object of xyz class named b(b=xyz()), and we set a=b. Also supposd In xyz's constructor there is a variable name y="abc" we modified it by a.y="Hello" and if we print(b.y) than also it gives "Hello", means the variable is actually being modified internally also. This is the same case in linked list, we did't performed any operation on head, but we were updating the reference through current/temporary node (cur). I suggest you to run this small code, your doubt will be more clear `class solution: def __init__(self): self.y='a' a=solution() b=a b.y='s' print(b.y) print(a.y)`
@nikhilhaspe2734
@nikhilhaspe2734 10 ай бұрын
This code does not work for [ 1,1,1 ] test case
@codingwithmx
@codingwithmx Жыл бұрын
Java solution: class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode current = head; if(head == null) return head; while(current.next !=null){ if(current.val == current.next.val){ current.next = current.next.next; }else{ current = current.next; } } return head; } }
@IamHiding24
@IamHiding24 2 жыл бұрын
Can someone please explain why we return head instead of cur? If the input of head is [1,1,2,3,3] wouldn't returning head return [1,1,2,3,3]? The code never modifies the head variable right? We only set cur = head. This is confusing
@pranavsharma7479
@pranavsharma7479 2 жыл бұрын
bro cur will be at null why return null, we will return head nah
@PETERPAN-b1f
@PETERPAN-b1f 2 жыл бұрын
You have same question as me...
@anishkhatiwada2502
@anishkhatiwada2502 2 жыл бұрын
I have replied to someone in the above comment section, if you are still confused you can check it.
@leeroymlg4692
@leeroymlg4692 2 жыл бұрын
as the code runs, cur will be incremented down the linked list and eventually cur will equal the last node in the list. If you return cur, you won't have the whole list, you will only get the last node of the list. If you return head, you will be returning the entire list. Cur != head.
@yfjsdgzjdnfn
@yfjsdgzjdnfn Жыл бұрын
The question return type is listnode which points to the revised list head if we return curr it is null which means your done with your question but not delivering the correct answer
@biswajeetpadhi100
@biswajeetpadhi100 Жыл бұрын
thanks
@IvanRadonjic-j9f
@IvanRadonjic-j9f 11 ай бұрын
My solution: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return head first = head second = first.next while second is not None: if first.val == second.val: temp = second.next first.next = temp second.next = None second = temp else: first = second second = second.next return head
@SHUBHAMKUMAR-ce8ix
@SHUBHAMKUMAR-ce8ix Жыл бұрын
idk why am i getting a time limit exceeded msg when my solution is literally the same as few solutions in the solutions section, anyone?
@JoseMejia-tp6od
@JoseMejia-tp6od Жыл бұрын
2 pointer solution var deleteDuplicates = function(head) { let left = head let right = head while (right) { if (left.val === right.val) { right = right.next continue } else if (left.next.val != right.val) { left.next = right left = left.next right = right.next } else { left = left.next right = right.next } } if (left && left.next) { left.next = right } return head };
@wasabi_san
@wasabi_san 3 жыл бұрын
Good solution but you could have also remove the appendages(references) of the duplicates to the original list so that garbage collection can do it's job. I know it's not in the problem requirements but one of the reasons we remove duplicates is to free up space but in your solution the same amount of space is still being occupied.
@MahmoudHassan-mm6gz
@MahmoudHassan-mm6gz 2 жыл бұрын
After making the assignment `cur.next = cur.next.next` there is no more references to the node that we "deleted" so the garbage collection will take care of that.
@hashtagaroma7778
@hashtagaroma7778 3 жыл бұрын
If the list was unsorted, what would have the time complexity been? Would it have been O(n^2)?
@sachinnair1613
@sachinnair1613 2 жыл бұрын
It would still be O(n). You could store the values you've seen already in a hash set, and check against that for every node you encounter. This way, you still only take one pass of the array, with a time complexity of O(n) and space complexity of O(n)
@octolocus5412
@octolocus5412 2 жыл бұрын
i love you
@bhanuvarma1239
@bhanuvarma1239 9 ай бұрын
This code will not working in geeks for geeks
Remove Duplicates from Sorted Array - Leetcode 26 - Python
10:38
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 456 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 170 М.
Reverse Linked List II - Leetcode 92 - Python
16:03
NeetCode
Рет қаралды 89 М.
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
12:19
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 341 М.
Intersection of Two Linked Lists - Leetcode 160 - Python
8:13
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 238 М.
LeetCode 82. Remove Duplicates from Sorted List II
6:40
Engineering Digest
Рет қаралды 2,5 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН