L18. Delete all occurrences of a Key in DLL

Поделиться
HTML-код
  • Опубликовано: 4 фев 2025

Комментарии • 80

  • @apoorvagupta4108
    @apoorvagupta4108 29 дней назад +1

    Thank you striver for teaching us so well that every concept remains in the mind as everything is crystal clear.

  • @alwayshemanthreddy
    @alwayshemanthreddy 10 месяцев назад +47

    Brother, update article because it will help us during revision

  • @akashverma5756
    @akashverma5756 Год назад +32

    Dummy node approach will alot simpler and also intuitive.

    • @suryapratapchaurasiya9198
      @suryapratapchaurasiya9198 7 месяцев назад

      can you provide the code and intution.

    • @anshpradhan1954
      @anshpradhan1954 7 месяцев назад +1

      BUT BUT BUT, in the dummy node approach you will have to use O(n) space as you store the valid LL in the dummy LL, so, Striver's approach is the same, in which you fiddle with the links with TC - O(n) and SC - O(1)

    • @anshpradhan1954
      @anshpradhan1954 7 месяцев назад +2

      @@suryapratapchaurasiya9198 the intuition is simple my friend, create a dummy node and then traverse through the original LL ok, and then in every iteration check if the node->data is equal to the key, if yes then dont append it to the dummy LL, if it is not equal append it to the dummy LL and in the end return the dummy->next as this will be the new LL head

    • @VikramSharma-tk5ob
      @VikramSharma-tk5ob 5 месяцев назад +2

      @@anshpradhan1954 hey ansh i dont think we are creating new nodes every time in the dummy node approach.. we will be just pointing dummy.next to the node whose data not equal to key . so no extra space complexity would be involved with this approach . i believe striver want to provide solution in a more new way. space complexity will still be O(1) as we are creating a single dummy node

    • @laraibanwr
      @laraibanwr 4 месяца назад

      @@suryapratapchaurasiya9198
      Node * deleteAllOccurrences(Node* head, int k) {
      Node* dummyNode = new Node(-1);
      Node* ptr = dummyNode;
      Node* temp = head;
      while(temp){
      if(temp->data != k){
      ptr->next = temp;
      temp->prev = ptr;
      ptr = temp;
      }
      else{
      Node* delNode = temp;
      delete delNode;
      }
      temp = temp->next;
      }
      ptr->next = NULL;
      head = dummyNode->next;
      delete dummyNode;
      return head;
      }
      TC: O(n)
      SC: O(1)

  • @LordOdin-z7m
    @LordOdin-z7m 4 месяца назад +3

    Updated the logic a bit.
    if you check the test cases, you would see that there are contiguous nodes with key hence, we can skip processing those nodes and directly jump to node where data is not equal to key
    Node curr = head;
    Node prevNode = curr.prev;
    while(curr != null){
    if(curr.data == x){
    prevNode = curr; // this is the node with key
    do{
    curr = curr.next;
    }while(curr != null && curr.data == x);
    // update the ptrs when you get node where data != key
    if(prevNode.prev == null){
    head = curr;
    }else{
    prevNode.prev.next = curr;
    }
    // curr can be null eg. consider DLL 5-3-2-2-2-2-2 and key =2
    if(curr != null){
    curr.prev = prevNode.prev;
    }
    }
    if(curr!= null){
    curr = curr.next;
    }
    }
    return head;
    TC -> O(N)
    SC -> O(1)

  • @Shreyess_23
    @Shreyess_23 Год назад +8

    Striver please bring a new series on heaps/stack and queque /strings or any other we're much awaited

  • @namamisingh6704
    @namamisingh6704 4 месяца назад +2

    (Not for the java people)
    when i looked through comments I came across many dummy approach solutions but in my opinion there is something missing mostly in the C++ code that is if the temp==key then you need to delete the node and Yes, but should store the nextnode beforehand to move to the next node idk if am wrong or right but writing this because I thought that the main motive of the question was to delete the space took by the repititive nodes and somewhere we missed the point.👍

  • @tusharmishra2205
    @tusharmishra2205 Год назад +3

    great video striver❤

  • @mohitejaikumar
    @mohitejaikumar 8 месяцев назад +5

    God of DSA

  • @rumiNITPatna
    @rumiNITPatna Месяц назад

    thank youuu stiver!

  • @saileela3
    @saileela3 Год назад +2

    Thank you

  • @dhruvsovasaria
    @dhruvsovasaria 3 месяца назад

    thankyou

  • @TON-108
    @TON-108 Год назад

    Understood Bhaiya!
    Thank You....

  • @Mel-up7un
    @Mel-up7un 4 месяца назад

    Understood Bhaiya thank you

  • @momentcoder
    @momentcoder Месяц назад

    Understood ✌✌

  • @om3478
    @om3478 Месяц назад

    understood!!!

  • @NehaKadam-t7c
    @NehaKadam-t7c Год назад +3

    Java Code:
    public class Solution {
    public static Node deleteAllOccurrences(Node head, int k) {
    // Write your code here.

    Node temp =head;
    while(temp!= null){
    if(temp.data==k){
    // to delete head
    if(temp==head){
    head= temp.next; // delete head
    }
    Node nextNode= temp.next;
    Node prevNode = temp.prev;
    if(nextNode!= null){
    nextNode.prev= prevNode;
    }
    if(prevNode!= null){
    prevNode.next= nextNode;
    }
    temp = nextNode;
    }
    else{
    temp= temp.next;
    }
    }
    return head;
    }
    }

  • @riyajadhav2905
    @riyajadhav2905 5 месяцев назад +4

    Pliz update the article in a2z DSA sheet

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 11 месяцев назад

    Thank you Bhaiya

  • @adarshveerabathini8550
    @adarshveerabathini8550 Месяц назад

    Hey guys we can use two pointers here starting from the head similarly like we did in the case of arrays to change the value in the node .it will be o(n) t.c and o(1) s.c .thankyou striver ❤.its because of u i got this approach into my mind

  • @PCCOERCoder
    @PCCOERCoder 2 месяца назад

    Lecture successfully completed on 29/11/2024 🔥🔥

  • @YourCodeVerse
    @YourCodeVerse Год назад

    Understood✅🔥🔥

  • @abhisweb2315
    @abhisweb2315 Год назад +1

    At 4:27 prevnode is moved from null to 4 ..but the condition for prev node to move forward is that prevnode!=null ... Here instead of being null the previous is moved to next .... Can anyone please help me😢 in understanding this ????

    • @Kshitijsingh-n7f
      @Kshitijsingh-n7f 5 месяцев назад

      first of all it is not for moving it is for updating the links only
      we update the value of the prevnode in above lines "prevnode=temp->prev"
      there is no restriction to update the value of the prevnode, only a check before updating the links

  • @DeadPoolx1712
    @DeadPoolx1712 4 месяца назад

    UNDERSTOOD;

  • @rajatshukla2605
    @rajatshukla2605 3 месяца назад

    Understood!

  • @nayankhuman1043
    @nayankhuman1043 4 месяца назад

    Understood ❤

  • @ArpitaChoudhury_00
    @ArpitaChoudhury_00 7 месяцев назад

    Thank You✨

  • @user-fw4kz3bb4g
    @user-fw4kz3bb4g 9 месяцев назад

    very easy dummy node approach
    Node * deleteAllOccurrences(Node* head, int k) {
    Node* dummynode = new Node(-1);
    dummynode->next=head;
    Node* temp=dummynode;
    while(temp->next!=nullptr){
    if(temp->next->data==k){
    temp->next=temp->next->next;
    if(temp->next!=nullptr)temp->next->prev=temp; //if tail element
    }else temp=temp->next;
    }
    return dummynode->next;
    }

  • @NazeerBashaShaik
    @NazeerBashaShaik 9 месяцев назад

    Understood, thank you.

  • @WorstinJava
    @WorstinJava 9 месяцев назад +1

    Bro , can we use predefined linkedlist or user-defined linkedlist in interview

  • @ganeshvhatkar9253
    @ganeshvhatkar9253 Год назад

    Understood

  • @ishanmoykhede9484
    @ishanmoykhede9484 7 месяцев назад +1

    my code ik it`s messier but still runs perfectly
    Node * deleteAllOccurrences(Node* head, int k) {
    // Write your code here
    while(head ->data == k){
    Node* temp = head ;
    head = head -> next ;
    if(head == nullptr) return nullptr ;
    head -> prev = nullptr ;
    temp -> next = nullptr ;
    delete temp ;
    }
    Node* mover = head -> next ;
    while(mover){
    if(mover -> data == k){
    if(mover -> next == nullptr ){// only if tail also has data equal to the given key value
    Node* back = mover -> prev ;
    back -> next = nullptr ;
    mover -> prev = nullptr ;
    delete mover ;
    return head ;
    }
    else {
    Node* back = mover -> prev ;
    Node* front = mover -> next ;
    back -> next = front ;
    front -> prev = back ;
    Node* temp = mover ;
    mover = front ;
    temp -> next = nullptr ;
    temp -> prev = nullptr ;
    delete temp ;
    }
    }
    else mover = mover -> next ;
    }
    return head ;
    }

  • @striverdaaadi
    @striverdaaadi Год назад

    understood sir

  • @subee128
    @subee128 Год назад

    Thanks

  • @dynamictip5251
    @dynamictip5251 Год назад +4

    //using dummy node java
    func(head){
    Node dummy =new Node(-1);
    Node curr=dummy;
    Node temp=head;
    while(temp!=null)
    {
    if(temp.data!=k)
    {
    curr.next=temp;
    temp.prev=curr;
    curr=curr.next;
    }
    temp=temp.next;

    }
    //what if last node which is next to curr is k
    curr.next=null;
    return dummy.next;
    }

  • @VivekKumar-kx2zf
    @VivekKumar-kx2zf 4 месяца назад

    why you are not removing links after deletion like you did while teaching the deletion at kth index in Doubly Linked List? Can we us this same approach there also? I am trying to do that here but that's not working i don't know why..

  • @Kshitijsingh-n7f
    @Kshitijsingh-n7f 5 месяцев назад

    why we need to check for prevnode!=NULL ? since prevnode is always not NULL na? after the first node(head)?
    Am i missing something?

  • @harshitjaiswal9439
    @harshitjaiswal9439 11 месяцев назад

    understood

  • @DecodeWithSatendra-shorts
    @DecodeWithSatendra-shorts 4 месяца назад

    Python Solution with Space Complexity: O(1)
    def deleteAllOccuranceOfX(head, x):
    temp = head
    if head is None:
    return None
    while(temp):
    next_pointer = temp.next
    if temp.data == x:
    #handle head
    if temp.prev is None:
    if head.next is None:
    return None
    temp.next.prev = None
    head = temp.next
    #handle tail
    elif temp.next is None:
    temp.prev.next = None
    else:
    temp.next.prev, temp.prev.next = temp.prev, temp.next
    temp = next_pointer
    return head

  • @TarunKumar-km5ps
    @TarunKumar-km5ps 10 месяцев назад +1

    amazing gurur ji

  • @State_exam_preparation
    @State_exam_preparation Год назад

    When we should expect the completion of this series?

  • @saidurgasrividyaupadhyayul4675
    @saidurgasrividyaupadhyayul4675 7 месяцев назад

    Reorder a list problem explanation please

  • @aayushgakhar3525
    @aayushgakhar3525 9 месяцев назад

    for the case where temp =head,why didnt u free up the old head after updation to new head??????

    • @VivekKumar-kx2zf
      @VivekKumar-kx2zf 4 месяца назад

      same question

    • @Mel-up7un
      @Mel-up7un 4 месяца назад

      Its because we are freeing the temp anyway after the if condition

    • @ictfan-ly8gh
      @ictfan-ly8gh День назад

      It is deleted . Our temp =head .
      So he moved head to head next .
      But has removed temp was containing head

  • @imPriyansh77
    @imPriyansh77 Год назад

    Thanks Bhaiya !!

  • @SurajKumar-ku1lg
    @SurajKumar-ku1lg 9 месяцев назад +1

    done myself with dummynode approach

  • @harshavardhan7074
    @harshavardhan7074 Год назад

  • @thelazycoder_789
    @thelazycoder_789 11 месяцев назад

    Dummy Node Approach:
    Node * deleteAllOccurrences(Node* head, int k) {
    //if head only null we cant do anything just return null
    if( head == nullptr) return nullptr;
    //taking dummy node and assign its next to head , back is null
    Node* dummy = new Node(-1 , head , nullptr);
    //head prev should me dummy now
    head->prev = dummy;
    //now traverse using cur until null
    Node* cur = head;
    while( cur != nullptr){
    //cur is k
    if(cur->data == k){
    //assign a prev and next ptr
    Node* prev = cur->prev;
    Node* nextNode = cur->next;

    //now linking prev to next node, prev is always exist so no need to check
    prev->next = cur->next;
    //if next node is null , cant do NULL->prev , so check before linking backwards
    if(nextNode != nullptr )nextNode->prev = cur->prev;
    }

    //now move cur , if cur element not equals it simply moves , if equals performs if and then moves, thats all.
    cur = cur->next;

    }
    //returning the new head.
    return dummy->next;
    }

  • @rishitkamboj8078
    @rishitkamboj8078 5 месяцев назад

    Node list2 = new Node(-1);
    Node t2 = list2;
    Node temp = head;
    while (temp != null) {
    if (temp.data != x) {
    t2.next = temp;
    temp.prev = t2;
    t2 = temp;
    }
    temp = temp.next;
    }

    t2.next = null;
    return list2.next;
    // just remove the -1 from the constructor then it will work on geeksforgeeks otherwise this approach is just fine

  • @_sf_editz1870
    @_sf_editz1870 8 месяцев назад

    a little optimised version of java class Solution {
    static Node deleteAllOccurOfX(Node head, int x) {
    // Write your code here
    while(head.data==x){
    head=head.next;
    if(head==null) return null;
    }
    //cutting the backward link
    head.prev = null;
    Node temp = head;
    while(temp!=null){
    if(temp.data==x){
    Node nextNode = temp.next;
    Node prevNode = temp.prev;
    //make links
    if(nextNode!=null) nextNode.prev = prevNode;
    if(prevNode!=null) prevNode.next = nextNode;
    //delete the links of temp
    temp.next = null;
    temp.prev = null;
    temp = nextNode;
    }
    else temp = temp.next;
    }
    return head;
    }
    }

  • @akshanshkaushal9422
    @akshanshkaushal9422 7 месяцев назад

    dummy list approach
    c++;
    Node * deleteAllOccurrences(Node* head, int k) {
    Node* temp = head;

    Node* dummyDll = new Node(-1);
    Node* dummy = dummyDll;

    while(temp != NULL){
    if(temp->data != k) {
    dummy->next = temp;
    temp->prev = dummy;
    temp = temp->next;
    dummy = dummy->next;
    }else{
    temp = temp->next;
    }
    }
    dummy->next = NULL;
    Node* ans = dummyDll->next;
    delete dummyDll;
    return ans;
    }

  • @priyamittal8302
    @priyamittal8302 9 месяцев назад

    Node * deleteAtStart(Node* head, Node* temp, Node* curr) {
    Node *newHead = head->next;
    newHead->prev = nullptr;
    delete head;
    return newHead;
    }
    Node * deleteAtEnd(Node* head, Node* temp, Node* curr) {
    temp->prev->next = nullptr;
    delete temp;
    return head;
    }
    Node * deleteAtMid(Node* head, Node* temp, Node* curr) {
    temp->prev->next = temp->next;
    temp->next->prev = temp->prev;
    delete temp;
    return head;
    }
    Node * deleteAllOccurrences(Node* head, int k) {
    Node *temp = head;
    while (temp != nullptr) {
    Node *curr = temp;
    temp = temp->next;
    if (curr->data == k) {
    if (curr->prev == nullptr) {
    head = deleteAtStart(head, curr, temp);
    } else if (curr->next == nullptr) {
    head = deleteAtEnd(head, curr, temp);
    } else {
    head = deleteAtMid(head, curr, temp);
    }
    }
    }

    return head;
    }
    Why is this giving runtime error in one of the test cases?

  • @jayadeepmohanty5212
    @jayadeepmohanty5212 Год назад

  • @cenacr007
    @cenacr007 9 месяцев назад

    us

  • @newdew13
    @newdew13 4 месяца назад

    Java solution with handling idle nodes
    //TC:O(N)
    //SC:O(1)
    static Node deleteAllOccurOfX(Node head, int x) {
    Node temp=head;
    while(temp!=null){
    if(temp.data==x){
    if(temp.prev==null){//head
    Node before=head;
    head=head.next;
    if(head!=null){
    head.prev=null;
    }
    before.next=null;
    temp=head;
    }
    else{
    Node before=temp.prev;
    Node after=temp.next;
    temp=temp.next;
    if(after!=null){//tail
    after.prev=before;
    }
    before.next=after;
    }
    }
    else{
    temp=temp.next;
    }
    }
    return head;
    }

  • @AkashBisht-cq3ys
    @AkashBisht-cq3ys 8 дней назад

    27 jan 2025 10:02pm

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 11 месяцев назад

    Thank you Bhaiya

  • @sibiranganath
    @sibiranganath 9 месяцев назад

    Understood

  • @chiragbansod8252
    @chiragbansod8252 10 месяцев назад

    understood

  • @hardikpatel352
    @hardikpatel352 8 месяцев назад

    Understood

  • @RaviKumar-sn6tu
    @RaviKumar-sn6tu 8 месяцев назад

    thank u bhaiya

  • @abhishekprasad010
    @abhishekprasad010 8 месяцев назад

    understood

  • @oyeshxrme
    @oyeshxrme 6 месяцев назад

    thank you bhaiya

  • @YPZanzarukiya
    @YPZanzarukiya 6 месяцев назад

    understood

  • @dewanandkumar8589
    @dewanandkumar8589 8 месяцев назад

    Understood

  • @abhinavabhi3568
    @abhinavabhi3568 3 месяца назад

    Understood

  • @meghasharma4875
    @meghasharma4875 4 месяца назад

    understood

  • @KrishMewari-y3k
    @KrishMewari-y3k 22 часа назад

    Understood

  • @himasreedadam7670
    @himasreedadam7670 Месяц назад

    understood