Remove Linked List Elements - Leetcode 203

  Рет қаралды 59,202

NeetCode

NeetCode

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Coding Solutions: • Coding Interview Solut...
Problem Link: leetcode.com/problems/remove-...
0:00 - Drawing Solution
5:12 - Coding Solution
leetcode 203
#python #linkedlist #leetcode

Пікірлер: 51
@NeetCode
@NeetCode 3 жыл бұрын
Linked List Playlist: kzbin.info/www/bejne/fWHCemCQe5WGaZo
@inarizakiFan
@inarizakiFan 6 ай бұрын
Damnn, that dummy node technique is very cool. It saves the hassle of updating the head pointer and dealing with 3 edge cases.
@mostinho7
@mostinho7 Жыл бұрын
Done thanks Using 3 pointers, current, previous and next Use a dummy node as head so the logic can be straightforward (avoids handling edge case of deleting the head of the list) Initialize prev to the dummy node, curr to head of list and next to node after it Pitfall:- When you delete a node, move forward current and next pointers while keeping previous in place Remember that you have a dummy head node so when returning the new list head you give dummy.nxt
@Graywolf116
@Graywolf116 2 жыл бұрын
You can use 1 pointer if you have a 2-layer while loop. The pointer and pointer.next effectively work like 2 pointers (you'll also need a '3rd' pointer to the new list's root). The 2nd-layer while loop handles consecutive target values by incrementing the next pointer until it gets to a non-target node. Then the pointer itself is incremented in the outer while loop. It's important to null-check first in this method, before checking for a target value. Your videos are a great resource btw!
@abhisheksunkale6672
@abhisheksunkale6672 2 жыл бұрын
#Without using prev just cur (accepted) def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: dummy = cur = ListNode() dummy.next = head while cur.next: if cur.next.val == val: cur.next = cur.next.next else: cur = cur.next return dummy.next
@smartsoothing776
@smartsoothing776 Жыл бұрын
Great Solution Bro
@niko-l-
@niko-l- 3 жыл бұрын
Awesome as always. Could you please create playlists for other data structures?. Personally, I find it very efficient to solve several problems related to one data structure and then switch to another. BTW, since you don't override curr variable you don't need to use nxt :) Thank you very much. I belive you will rock in subscribers very soon
@rameshdhas
@rameshdhas 3 жыл бұрын
Great tutorials..sharp to the point..two thumbs up!!
@varuntyagi6116
@varuntyagi6116 2 жыл бұрын
Recursive solution def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: if head is None: return None next = self.removeElements(head.next, val) if head.val == val: head = next else: head.next = next return head
@onkarjaliminche1756
@onkarjaliminche1756 3 жыл бұрын
Thanks, bro, it's helping me a lot, much appreciated
@singletmat5172
@singletmat5172 3 жыл бұрын
Love the consistent uploads, ty and keep up the good work!
@NeetCode
@NeetCode 3 жыл бұрын
Thanks, that means alot :~)
@rohithjanardhan4970
@rohithjanardhan4970 2 жыл бұрын
Amazing solution, tysm!
@candiceliu5664
@candiceliu5664 2 жыл бұрын
You have no idea how much this video has helped me with my assignment!! Thank you so much!! xxxx
@xgedokid1412
@xgedokid1412 2 жыл бұрын
Why does the prev.next = curr.nxt affect the values in dummy node? I assumed it wasn't going to affect the dummy node because it was just a copy.
@shurale85
@shurale85 8 ай бұрын
In the name of Lord. It is all about reference type mechanism. Initially prev and dummy has the same ref so changing prev reflects on dummy. However, once prev is changed to cur (prev = cur) dummy and prev have the different ref. That's why first iteration changes prev and dummy, then prev takes cur and no more connected to dummy (if we consider the case where first element has to be deleted) I also was stuck first and needed to dig into. Peace on u! Stop genocide in Palestine!
@joshuamarcano350
@joshuamarcano350 2 жыл бұрын
What app are you using to black board?
@johanesalberto6136
@johanesalberto6136 23 күн бұрын
thank you so much
@AshishGupta-be2yz
@AshishGupta-be2yz Жыл бұрын
nice explaination😄
@benyaminyakobi3652
@benyaminyakobi3652 3 жыл бұрын
Thank you
@omerfarukozturk9720
@omerfarukozturk9720 Жыл бұрын
Teşekkürler!
@kyranzhanat9721
@kyranzhanat9721 2 жыл бұрын
thanks bro!
@aydinakbuga
@aydinakbuga Жыл бұрын
thanks a lot
@abhishekmorla1
@abhishekmorla1 2 жыл бұрын
for java people : class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummyhead = new ListNode(); // act as a previous node ListNode tail = dummyhead; dummyhead.next = head; ListNode temp = head; while (temp!=null) { if (temp.val == val) { tail.next = temp.next; } else{ tail = temp; } temp = temp.next; } return dummyhead.next; } }
@Shanky_17
@Shanky_17 3 жыл бұрын
i love when u say neetcode problem
@yingxie9974
@yingxie9974 2 жыл бұрын
there is a bug in this video while(cur) { ListNode* nxt = cur->next; if(cur->val == val) pre->next = nxt; else { pre->next = cur; >>> this step is ignored in this video (7:11) pre = cur; } cur = nxt; }
@LazyLatte_27
@LazyLatte_27 3 жыл бұрын
If there was 1 at the last position of the list, wouldn’t the dummy node now point to the last node(after removing 1)?
@akishu123
@akishu123 2 жыл бұрын
we are not touching the dummy.next it is always pointing to the head of the linked list . dummy.next is going to change ONLY if continuous start positions got the value which we have to remove.
@merissabridgeman1552
@merissabridgeman1552 22 күн бұрын
Is there something I'm missing in the explanation? This makes sense but when I try this solution I keep running into an attribute error where the list doesn't support the next attribute
@aynuayex
@aynuayex 7 ай бұрын
here is a solution with out creating a dummy node prev, cur = head, head while cur: if head.val == val: head = head.next prev, cur = head, head elif cur.val == val: prev.next = cur.next cur = prev.next else: prev = cur cur = cur.next return head your solution super useful, but I guess we don't need nxt dummy = ListNode(next=head) prev, cur = dummy, head while cur: if cur.val == val: prev.next = cur.next else: prev = cur cur = prev.next return dummy.next
@ericnarciso9156
@ericnarciso9156 2 жыл бұрын
I am not sure I understand completely. What if in line 15: curr.next.val == val ?
@ericnarciso9156
@ericnarciso9156 2 жыл бұрын
never mind. I see: prev.next would be triggered again and so it would skip that value as well.
@haochenyu2163
@haochenyu2163 2 жыл бұрын
Before watching this channel: LeetCode preminum After watching this channel: Goodbye monthly subscription
@baseballMMAgames
@baseballMMAgames Жыл бұрын
If the node you are removing is the head, why would you not just make the head the next element and then nothing changes except the previous head falls off the linked list?
@inarizakiFan
@inarizakiFan 6 ай бұрын
because you will need to write extra two or three lines to update the head pointer each time if the elem to remove is head, it is more elegant and simpler to use the dummy node thing.
@ankursingh9669
@ankursingh9669 9 күн бұрын
why "prev.next = current " is not working ,it should also work
@lee_land_y69
@lee_land_y69 2 жыл бұрын
why returning head is wrong?
@Thomas-vm1jy
@Thomas-vm1jy 2 жыл бұрын
This solution doesn't work, all it does is point the prev point to current when it finds a value match in the middle
@coolkid9206
@coolkid9206 Жыл бұрын
I don’t understand why he had while curr: as the while loop. Can someone please explain this to me?
@gagemachado2597
@gagemachado2597 Жыл бұрын
this is how you itterate through the linked list, on line 19 he sets curr to the next value and it will continue to the next value until it is NULL ie. at the end of the linked list and the while loop will end.
@abhisheksinghchauhan6583
@abhisheksinghchauhan6583 Жыл бұрын
For JAVA people class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(); ListNode prev = dummy; dummy.next = head; ListNode curr = head; while(curr != null ) { if(curr.val == val) { prev.next = curr.next; } else { prev = curr; } curr = curr.next; } return dummy.next; } }
@researchandbuild1751
@researchandbuild1751 2 жыл бұрын
This is confusing you say we use two pointers and it seems like the dummy is one of the pointers but then you talk about moving both pointers every time , well then how could dummy point to the head? Sorry i think you are not clear enough
@amanrai5285
@amanrai5285 Жыл бұрын
by using the listnode class we are building duplicate linked list awith nor values and then we dynamicallu add the values of that node using the prev and the cur pointer as you can see that the values in the current node has the actual head value that we are comparing other than that we are just using prev to update the values
@aparnam6330
@aparnam6330 2 жыл бұрын
how is dummy.next is getting updated since we are never updating it?
@webknowledge9989
@webknowledge9989 2 жыл бұрын
It points to head which _is_ getting updated.
@mohitsharma38
@mohitsharma38 2 жыл бұрын
@@webknowledge9989 where is the head getting updated only prev and curr are getting updated and curr has copy of head if curr is getting updated than how it can update the head
@fahadfreid1996
@fahadfreid1996 2 жыл бұрын
@@mohitsharma38 it's important to understand that you are not creating copies at any point here. The variables are just references to the actual objects (i.e head). You are modifying the contents of the object whenever you edit the object's properties like next and val.
@shahzebahmad7866
@shahzebahmad7866 2 жыл бұрын
public Node removeNode(Node head,int data){ Node temp = head; if(temp.data==data){ head= head.next; temp = head; } else{ while(temp.next!=null){ if(temp.next.data==data){ temp.next = temp.next.next; } else temp = temp.next; } } return head; }
@cfbf96
@cfbf96 Жыл бұрын
this is wrong, if the first two values need to be removed, this would not work.
@cutieazimiz
@cutieazimiz Жыл бұрын
what do you think of this Python solution? I know it's O(n) time and space complexity but this was my first attempt :) nums = [ ] dummy = ListNode() current = dummy while head: nums.append(head.val) head = head.next for num in nums: if num != val: current.next = ListNode(num) current = current.next return dummy.next
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 400 М.
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 10 МЛН
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
Palindrome Linked List - Leetcode 234 - Python
11:06
NeetCode
Рет қаралды 91 М.
Why The Windows Phone Failed
24:08
Apple Explained
Рет қаралды 220 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 288 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
Linked Lists for Technical Interviews - Full Course
1:27:24
freeCodeCamp.org
Рет қаралды 351 М.
Новые iPhone 16 и 16 Pro Max
0:42
Romancev768
Рет қаралды 2,4 МЛН
Bluetooth connected successfully 💯💯
0:16
Blue ice Comedy
Рет қаралды 1,5 МЛН
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 8 МЛН