LeetCode 203. Remove Linked List Elements Solution Explained - Java

  Рет қаралды 37,195

Nick White

Nick White

Күн бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
AlgoCademy - algocademy.com...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/...
Follow Me on X/Twitter - x.com/nickwhit...
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nick....
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 60
@researchandbuild1751
@researchandbuild1751 2 жыл бұрын
Best explanation and the logic makes the most sense. Other videos i have found sucked at explaining this
@ngndnd
@ngndnd Жыл бұрын
i saw ur comment on the neetcode video and i completely agree lmaooo
@jayachandra677
@jayachandra677 3 жыл бұрын
I tried it many times without watching any of the solutions and then I came to this video and realized that I had to move the head pointer forward until the values are not equal before performing any other operations. Thank you!
@ssrpic
@ssrpic 2 жыл бұрын
This happened with me too
@hassansmallah
@hassansmallah 4 жыл бұрын
same here Failed for input: 1-> 2-> 2-> 1 i think it's a new test case. cheers
@de_vamp
@de_vamp 4 жыл бұрын
Nope its working fine for me
@jugsma6676
@jugsma6676 4 жыл бұрын
Yea, that test-case didn't work
@HafsaHaqSarker
@HafsaHaqSarker 2 жыл бұрын
I had the same issue, are you sure you put an else { curr = curr.next} ?
@Omniescience
@Omniescience 4 жыл бұрын
Why does saying “current_node.next = current_node.next.next” line 18 change the head linked list, but saying “current_node = current_node.next” line 20 does not? If changing current_node has an effect on the head, and current node is getting smaller as you traverse until you get to null. Maybe it’s just something I don’t understand about traversing linked list. This is a new data structure to me.
@researchandbuild1751
@researchandbuild1751 2 жыл бұрын
Current-node doesnt affect head at all, its a copy, it just starts at head and moves forward
@lillang8812
@lillang8812 2 жыл бұрын
@@researchandbuild1751 they're not only a copy they're literally the same instance. You can make an instance of a class num with one attribute: value. and you make an instance called A with a value of 5. you then make an instance called B and set it equal to A. if you change the value of A it will change the value of B as well.
@FractalOut
@FractalOut 3 жыл бұрын
I don't understand why we are returning head, if we are changing values within current. Can someone please explain this to me?
@zhixiangren4580
@zhixiangren4580 3 жыл бұрын
I think it's because current_node is just the address of the first node but not the "whole linked list" . same for "head", it's just an address. we went through the linked list with "current", so in the end, if we return current, it will only give us the last number instead of the linked list
@nandinisraghavan8788
@nandinisraghavan8788 2 жыл бұрын
Same qs i have
@lillang8812
@lillang8812 2 жыл бұрын
@@zhixiangren4580 nah its the address of the whole linked list, its just that current_node has traversed its pointer and head hasn't
@jagrutitiwari2551
@jagrutitiwari2551 2 жыл бұрын
Maybe because the head is pointing to the first address. While current_node is traversing the list and removing the elements. Head stays intact while current_node is doing the job. In the end, current_node will point only to the last element
@XAyaDubX
@XAyaDubX 2 жыл бұрын
This video helped me on an important assignment. Thank you very much!
@shrirambalaji2915
@shrirambalaji2915 Жыл бұрын
I initially solved the problem, but I ignored one possibility: what if head.value is value. Thank you for the answer, buddy.
@Rob-J-BJJ
@Rob-J-BJJ Жыл бұрын
so you didnt solve the problem
@shrirambalaji2915
@shrirambalaji2915 Жыл бұрын
@@Rob-J-BJJ I did.
@siuwan3406
@siuwan3406 3 жыл бұрын
Hi,Nick! I have a question about why you need to return head at the end of the function?
@kaushikramabhotla4635
@kaushikramabhotla4635 3 жыл бұрын
coz its a function with a return type of ListNode
@Darkstorm12321
@Darkstorm12321 2 жыл бұрын
It is also just a convention of the LeetCode problem. You return the head of the list and it is like you are returning the new list (in the sense that you are returning the start of it); you cannot return the current node because Leetcode would not interpret this as the entire new list
@katieghaemi8693
@katieghaemi8693 2 жыл бұрын
@@Darkstorm12321 I am also a bit confused why we return head. We did not modify head after line 16, therefore why do we return head at the end? The only thing that was modified after line 16 was current_node.
@Darkstorm12321
@Darkstorm12321 2 жыл бұрын
@@katieghaemi8693 I hope I explain this correctly. Anyone else, feel free to jump in So in the beginning, your list is 1-> 2 -> 6 -> 3 -> 4 -> 5 -> 6 H Your head is the pointer to the beginning of the list, which is 1. You do not move that. Instead, you have another pointer called current_node. This is what you move along. This also is what identifies the node to delete, breaks the connection around the target, and leaves you with this 1->2->3->4->5 H Now what do you return? You wouldn't return your current_node, as this is a pointer that has traversed the list. If we returned that, leetcode would "fail" you because it would look like only the end of the list. Instead, you return head because it is a pointer to the start of the list. It is LIKE you are returning the entire linked list. Another way to think of it is like a string. Say you have the word "hello," but instead of a string it is a contiguous array of characters. You can get a pointer to the first character in the string, and that is LIKE working with the string itself.
@Robloxangel574
@Robloxangel574 2 жыл бұрын
@@Darkstorm12321 I was trying to get an answer to the same question here, well explained, thank you!!!
@varshilanavadia
@varshilanavadia 3 жыл бұрын
If you interchange the conditions in the while loop, that is... while(head.val != val && head != null) { // do something } Leetcode throws a runtime exception for a test case when you submit the code. Anyone know why ? Does it have to do with how conditions within the while loop are evaluated ? The conditions are the same, just the order is reversed, what's wrong with that ?
@anshulmittal6085
@anshulmittal6085 3 жыл бұрын
Yes, while checking the AND conditions, it checks the 1st condition, if it is true, then only it checks for the 2nd condition. So, if head = null, then head.val will throw exception (it's nullpointerexception), as this is the 1st condition you are writing
@madanmohan5661
@madanmohan5661 2 жыл бұрын
while (head != NULL && head->val == val) why can't we use here if instead of while? and, while (curr != NULL && curr->next != NULL) why we have to check these both conditions?
@qusai2062
@qusai2062 2 жыл бұрын
what about the conditions ? . it is strange that test case of "The number of nodes in the list is in the range [0, 104]." didn't appear after summation
@Rob-J-BJJ
@Rob-J-BJJ Жыл бұрын
thanks i solved the problem i just didnt put it in an if else condition so i was getting adjacent elments wrong
@anirudhramprasad2976
@anirudhramprasad2976 4 жыл бұрын
This program will not work for 2 consecutive values in a list which have the value val. Say in a list with values [2,4,4,2] for example, if the current node carries the value 2, the program will remove the second node with the value 4 and current will take the next value of 4. Because of this the second value of 4 will not be checked against val leaving it in the list.
@Name-pn5rf
@Name-pn5rf 4 жыл бұрын
same
@Name-pn5rf
@Name-pn5rf 4 жыл бұрын
did you find any solution
@bradleyberthold4606
@bradleyberthold4606 2 жыл бұрын
You are incorrect, it works fine, just tested it today
@anmoljaising
@anmoljaising 3 жыл бұрын
What's the need for lines 11-12? @nickwhite
@user-ju4bv9wh5o
@user-ju4bv9wh5o 3 жыл бұрын
the case where the value of head is equal to the value we want to delete
@ankushchhabra02
@ankushchhabra02 Жыл бұрын
thanks for explaining this solution so well :)
@akankshakanjolia2969
@akankshakanjolia2969 4 жыл бұрын
Thanks man!! it was very clean and easy approach
@nrico6666
@nrico6666 Жыл бұрын
For anyone learning this method note that this method is an extemely hacky method, if you work with something like C/C++ and the node is heap allocated it is not possible to free() the node memory and will result in memory leak. And this code also has hacky logic it will never be used in production.
@smartswaggy6114
@smartswaggy6114 2 жыл бұрын
Why this code doesnt work in C++ ListNode* removeElements(ListNode* head, int val) { while(head!=NULL and head->val==val){ return head->next; } ListNode* current=head; while(current!=NULL and current->next!=NULL){ if(current->next->val==val){ current->next=current->next->next; } else{ current=current->next; } } return head; }
@kuuly3334
@kuuly3334 2 жыл бұрын
This is for java not c++
@leonhunter7276
@leonhunter7276 2 жыл бұрын
ultra elegance. well done
@abtaheefarhanador6403
@abtaheefarhanador6403 3 жыл бұрын
wont work for adjacent vals
@Rob-J-BJJ
@Rob-J-BJJ Жыл бұрын
yes it does boron
@Glam1877
@Glam1877 Жыл бұрын
why do you define the current_node as another node and dont write just head?
@konstantybielewicz7476
@konstantybielewicz7476 Жыл бұрын
Because you want to return the head
@doloreshaze10
@doloreshaze10 3 жыл бұрын
what's a dummy head? thanks
@ameytarfe9597
@ameytarfe9597 5 жыл бұрын
Failed for input: 1-> 2-> 2-> 1
@de_vamp
@de_vamp 4 жыл бұрын
Its working
@oneofvru
@oneofvru 4 жыл бұрын
@@de_vamp no it's not.
@Name-pn5rf
@Name-pn5rf 4 жыл бұрын
yhh it fails for consecutive numbers
@teksomegeek2062
@teksomegeek2062 3 жыл бұрын
put currentnode=currentnode->next in else condition, then it should be fine.
@Rob-J-BJJ
@Rob-J-BJJ Жыл бұрын
@@oneofvru you need to learn proper computer science, it works
@chenchangyuan6551
@chenchangyuan6551 4 жыл бұрын
Thanks, but I got a problem that mine complexity is not good as yours well my code is the same as yours. XD
@user-ju4bv9wh5o
@user-ju4bv9wh5o 3 жыл бұрын
that could be a lot of different factors. because you're not running the code on the exact same machine and internet as his then your speeds could differ
@simranarora2817
@simranarora2817 2 жыл бұрын
Good job
@Khushi-ek6ii
@Khushi-ek6ii 4 жыл бұрын
great
Remove Linked List Elements - Leetcode 203
7:51
NeetCode
Рет қаралды 63 М.
LinkedList vs ArrayList in Java Tutorial - Which Should You Use?
11:43
Coding with John
Рет қаралды 597 М.
«Кім тапқыр?» бағдарламасы
00:16
Balapan TV
Рет қаралды 244 М.
Миллионер | 1 - серия
34:31
Million Show
Рет қаралды 2,7 МЛН
小天使和小丑太会演了!#小丑#天使#家庭#搞笑
00:25
家庭搞笑日记
Рет қаралды 56 МЛН
Officer Rabbit is so bad. He made Luffy deaf. #funny #supersiblings #comedy
00:18
Funny superhero siblings
Рет қаралды 18 МЛН
I Got Rejected (again)
9:43
Nick White
Рет қаралды 204 М.
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1 МЛН
Coding Was Hard Until I Learned THESE 5 Things!
8:02
Nick White
Рет қаралды 30 М.
Singly Linked List | Insert, Delete, Complexity Analysis
14:39
Blue Tree Code
Рет қаралды 60 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 444 М.
Coding Is Changing...Here Is What You NEED To Know
9:26
Nick White
Рет қаралды 64 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 315 М.
«Кім тапқыр?» бағдарламасы
00:16
Balapan TV
Рет қаралды 244 М.