LeetCode 203. Remove Linked List Elements Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Best explanation and the logic makes the most sense. Other videos i have found sucked at explaining this

researchandbuild
Автор

same here Failed for input: 1-> 2-> 2-> 1

i think it's a new test case. cheers

hassansmallah
Автор

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.

Omniescience
Автор

I don't understand why we are returning head, if we are changing values within current. Can someone please explain this to me?

FractalOut
Автор

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!

jayachandra
Автор

This video helped me on an important assignment. Thank you very much!

XAyaDubX
Автор

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

qusai
Автор

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 ?

varshilanavadia
Автор

I initially solved the problem, but I ignored one possibility: what if head.value is value. Thank you for the answer, buddy.

shrirambalaji
Автор

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?

madanmohan
Автор

Hi, Nick! I have a question about why you need to return head at the end of the function?

siuwan
Автор

What's the need for lines 11-12? @nickwhite

anmoljaising
Автор

thanks i solved the problem i just didnt put it in an if else condition so i was getting adjacent elments wrong

Rob-J-BJJ
Автор

thanks for explaining this solution so well
:)

ankushchhabra
Автор

Thanks man!! it was very clean and easy approach

akankshakanjolia
Автор

why do you define the current_node as another node and dont write just head?

Glam
Автор

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.

anirudhramprasad
Автор

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){

}
else{
current=current->next;
}

}
return head;
}

smartswaggy
Автор

Thanks, but I got a problem that mine complexity is not good as yours well my code is the same as yours. XD

chenchangyuan
Автор

Failed for input: 1-> 2-> 2-> 1

ameytarfe