Swap nodes in a linked list without swapping data (Very Easy Explanantion)

Поделиться
HTML-код
  • Опубликовано: 9 окт 2024
  • We are going to swap two nodes in the linked list by changing their pointers.

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

  • @arugollu
    @arugollu 4 года назад +24

    I was struggling to understand swapping logic today. Your explanation and drawing helped me understand. You do not assume anything and you explain from scratch. Thanks a lot for your videos. You are one of my teachers to master datastructures.

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

    After a year and a half of learning and practicing pointers and linked lists, one would expect to be able to seamlessly understand these kinds of things, but to my surprise it's still really hard to grasp.
    Pointers are confusing alone, but when swapping nodes, it's another story. And it can get even crazier: just imagine doing this on doubly linked lists... Or in circular lists! OR IN TREES!!!
    Anyways, I needed this to make a sorting function for linked lists, but didn't get it right until I saw this video. You kept all the details and mantained a slow pace in your explanation, just what I needed. Many teachers could learn from this... Thank you very much sir!

  • @Rahul-vl1no
    @Rahul-vl1no 2 года назад

    the way you say hello friends to till end of this video is really wonderful

  • @digivibe2404
    @digivibe2404 3 года назад +3

    I really enjoy your way of explaining the concepts of swapping nodes in. I have subscribed and will be watching all your lectures. Thank you sir.

  • @Anand14527
    @Anand14527 3 года назад +1

    This topic was really tricky and tough to understand but u made it very easy!

  • @icemotion1925
    @icemotion1925 3 года назад +2

    Thank you very much sir. You're a very good teacher, the step-by-step drawing really helpmed me.

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

    For adjacent nodes the swapping logic is slightly different:
    temp = pY -> next
    pY -> next = pX (without next)
    pX -> next = temp
    prevX -> next = pY
    You don't need to modify prevY

  • @redone729
    @redone729 3 года назад +1

    This is the best explanation I have seen. But this solution will not work if Y > X or if the two nodes is next to each other.

    • @sumitbaloda8561
      @sumitbaloda8561 3 года назад

      yeah! i already checked that

    • @sorrowdada
      @sorrowdada 2 года назад

      that's a swap function. You have to check that in your handler function not in Swap()

  • @manjunathm7632
    @manjunathm7632 6 лет назад +7

    What if X and Y are adjuscent to each other like a->b or c->d will this logic works

    • @jyotirmoy_dey
      @jyotirmoy_dey 6 лет назад +1

      It doesn't work in such case

    • @kas2nex
      @kas2nex 6 лет назад +1

      @vivekanand please reply
      We need your help!!!

    • @praveennarvaneni2970
      @praveennarvaneni2970 5 лет назад +2

      //This is the code which can handle two adjacent nodes either but the function assumes that first node occurs first and second node there after
      struct node *Swapnodes(struct node *head, struct node *first, struct node *second)
      {
      if (head == NULL)
      {
      //printf("
      You are given a null header to swap nodes
      ");
      return NULL;
      }
      if (first == second)
      return head;
      //printf("
      Can u see us I am head:%u I am first first:%u, I am second:%u
      ", head, head, second);
      struct node *temp;
      struct node *prev1 = NULL;
      struct node *prev2 = NULL;
      int found = 0, flag = 0;
      if (head == first)
      found = 1;
      for (temp = head; temp != NULL && !found; temp = temp->next)
      {
      printf("
      I am searching second, temp is :%u", temp);
      if (temp->next == first)
      {
      found = 1;
      prev1 = temp;
      }
      }
      if (!found)
      {
      printf("
      First not found
      ");
      return NULL;
      }
      found = 0;
      for (temp = head; temp != NULL; temp = temp->next)
      {
      printf("
      I am searching second, temp is :%u", temp);
      if (temp->next == second)
      {
      found = 1;
      prev2 = temp;
      break;
      }
      }
      if (!found)
      {
      printf("
      Second is not found
      ");
      return NULL;
      }
      if (!prev2)
      {
      printf("Unfortunately I found prev2 as null");
      return NULL;
      }
      if (prev2 == first)
      {
      flag = 1;
      }
      if (prev1 != NULL)
      {
      temp = second->next;
      prev1->next = second;
      if (flag)
      {
      second->next = first;
      }
      else
      {
      second->next = first->next;
      prev2->next = first;
      }
      first->next = temp;
      return head;
      }
      temp = second->next;
      if (flag)
      {
      second->next = first;
      }
      else
      {
      second->next = first->next;
      prev2->next = first;
      }
      first->next = temp;
      return second;
      }

  • @parampampam4477
    @parampampam4477 5 лет назад +4

    @
    Vivekanand Khyade - Algorithm Every Day
    In case of (prevY=null) should it be prevX->next=pY ( not prevX->next=prevY)?

    • @ajayrajsingh7926
      @ajayrajsingh7926 4 года назад +1

      yes

    • @niru1931
      @niru1931 4 года назад

      Yes , right

    • @sayantaniguha8519
      @sayantaniguha8519 3 года назад

      I think you are right. Where can I have the complete code?

    • @parampampam4477
      @parampampam4477 3 года назад

      @@sayantaniguha8519 Unfortunately, I don't have it, but you can code it yourself since the author explained the concept quite good

    • @sayantaniguha8519
      @sayantaniguha8519 3 года назад

      @@parampampam4477 ha woh toh sir ka dekh ke ban jayega...
      Par phir bhi sir ka code samjhe ni hum

  • @yx2302
    @yx2302 6 лет назад +4

    Really good class! Make it really clear!

  • @raviPrakash-zf2ze
    @raviPrakash-zf2ze 3 года назад +1

    u solved my problem ,explained very well thanks alot🙏🙏🙏

  • @manideeptaghosh7158
    @manideeptaghosh7158 2 года назад

    there is a problem . you have told the search function is a void type so it will return nothing.so if it returns nothing then how will those pointers prevX,prevY,pX,pY will work in the swap function. please explain

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

    very nicely explained..Thanks

  • @marell0451
    @marell0451 4 года назад

    Сижу в 3 часа ночи и делаю по этому видосу лабу для Париматча и попутно смотрю танец сортировок. Всем советую, хороший видос.

    • @Rusekk-
      @Rusekk- 4 года назад

      Ушастым привет!

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

    Very good explanation

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

    Very well explained 🙏🏻

  • @anindyasundarhazra2523
    @anindyasundarhazra2523 4 года назад +1

    Nice sirrrrrr. Very good explanation

  • @__noob__coder__
    @__noob__coder__ 3 года назад

    Nicely done. Much Appreciation for you !

  • @RAJESHKUMAR-eb5bh
    @RAJESHKUMAR-eb5bh 6 лет назад +1

    if *a* is replaced by *b* ? Or pY == pX.next?

  • @vaibhavbansal8205
    @vaibhavbansal8205 3 года назад +1

    Your code is wrong if you want to swap to corresponding nodes then it will not work

  • @AnkitSaiyan
    @AnkitSaiyan 5 лет назад +2

    can we make a program which work for all three cases

  • @niru1931
    @niru1931 4 года назад +2

    There is a mistake if (pervY==NULL) Than it will be prevX->next=pY not prevX->next=prevY

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

    Nice 👍 I Understand Thanks 😊

  • @madanjeetkumar1108
    @madanjeetkumar1108 6 лет назад +1

    sir please upload video on the topic "code explaination of how will you make binary tree using inorder and preorder ,inorder and postorder".

  • @mahesmahi1494
    @mahesmahi1494 5 лет назад +1

    It is not working to swapping of A node and B node so Check it once

  • @tilakrajchoubey5534
    @tilakrajchoubey5534 2 года назад

    I don't think it would work for adjacent nodes. Correct me someone if I am not.

  • @vrushankdoshi7813
    @vrushankdoshi7813 4 года назад +1

    Nice explanation.. but the solution will not work if we have a linked list as 1-2-3-4-null and your x = 4 and y = 1 instead of x= 1 and y =4

  • @ShivaniSingh-mq4pv
    @ShivaniSingh-mq4pv 3 года назад +1

    Nice explanation

  • @yaredhailu1286
    @yaredhailu1286 5 лет назад +2

    dear please help me implementation code for sorting double linked list

  • @holyshit922
    @holyshit922 6 лет назад

    Suppose we have given pointers to the nodes we want to swap and our linked list is doubly linked list
    How would our code look like

  • @reubenmammen8065
    @reubenmammen8065 4 года назад +1

    Amazing Teaching

  • @036_ankitjha5
    @036_ankitjha5 Год назад

    Too good..thnku❤

  • @abilvap5611
    @abilvap5611 3 года назад

    Thank you very much! everything is perfect

  • @icymatress3006
    @icymatress3006 2 года назад

    does this work for adjacent nodes?

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

    just amazing ..

  • @shikharsrivastava7312
    @shikharsrivastava7312 3 года назад

    sir but their can be duplicates nodes

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

    Dear sir, it doesn't work for adjacent nodes

  • @roshanrajkumar7827
    @roshanrajkumar7827 4 года назад

    Sir can u upload a video on sorting a linked list

  • @AboutCodeforcesProblem
    @AboutCodeforcesProblem 2 года назад

    Do you have source code?

  • @sivaganesh4489
    @sivaganesh4489 4 года назад

    You're just awesome

  • @ocochosi
    @ocochosi 4 года назад

    Good job :) thx

  • @sachinbhalla14
    @sachinbhalla14 5 лет назад +2

    sir your code is not working please give source code

  • @gautamsingh9175
    @gautamsingh9175 5 лет назад

    sir, if you want to increase your viewer you must add some background sound in video cause, it will help to motivate while we watch your videos ....i think you apply my suggestion in you'r videos thank for you'r video it help a lot..

  • @supamdeepbains5172
    @supamdeepbains5172 6 лет назад

    man u teach good but please don't do fake English excent

  • @ashifkasala6897
    @ashifkasala6897 6 лет назад

    Jus to remember me i disliked video.Sorry