Reverse a Linked List Code/Algorithm/Program

Поделиться
HTML-код
  • Опубликовано: 9 окт 2024
  • Given a Singly Linked List. Reverse the Linked list.

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

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

    Superb explanation Sir.
    Here's another approach of reversing a linkedlist recursively-
    private static LinkedListNode reverseLinkedList(LinkedListNode start, LinkedListNode prev) {
    if (start == null || start.next == null) {
    start.next = prev;
    return start;
    }
    LinkedListNode second = start.next;
    start.next = prev;
    return reverseLinkedList(second, start);
    }

  • @sarthaksrivastav3408
    @sarthaksrivastav3408 5 лет назад +5

    Your visualization thing is very helpful. Thank you.

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

    Sir interview me web project ke liye kis type ke project se kaam chl sakta h or jada problem na ho.
    1. Complete mern stack project.
    2. Any simple react project
    3. Simple With html,css or javascript.
    4. Any dsa project
    5. Or any dbms.
    Inme se konse rakha acha rhega or sirf kitno se kaam ho jayega pls reply.

  • @priyankamorankar8093
    @priyankamorankar8093 7 лет назад +2

    From this code it seems, "p->next->next = p" is getting executed only once. But it should be executed number of times recursive function is executed. Could you please explain this?

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

      it'll execute several of times until the recursive stack don't get empty, its hard to digest but this is how recursive work

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

    Amazing , worth watching, thank you sir , khup madat zali..... Parat ekda dhanyawad..... Banvat rha videos...

  • @dr.padmajapulicherla5545
    @dr.padmajapulicherla5545 5 лет назад

    Sir....all your videos are so good...you write code first on board and explain in a beautiful way....thank you...Dr.Padmaja.Pulicherla

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

    but if we return q after breaking at if(q==NULL) then how we can get head of reversed list?

  • @20092009ashish
    @20092009ashish 7 лет назад +16

    if(q==null) it should return p;

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

    We can simplify his code a little bit if we assume that we have lazy boolean evaluation
    node *reverse(node *head)
    {
    node *p, *q;
    if(head == NULL || head->next == NULL)
    return head;
    p = head;
    q = p->next;
    q = reverse(q);
    p->next->next = p;
    p->next = NULL;
    return q;
    }

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

    Please Sir can we change this instruction :
    P->next->next =p
    By
    q->next = p

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

    sir u have described the function which returns a node ,then how can u simply write return without mentioning any pointer along with it.At last how will we able to know the address of new head??

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

      make some changes --
      return head;
      in both the statements(3rd & 7th)

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

      Try this
      ruclips.net/video/S5UByEqe8aM/видео.html

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

    Can you please make solution video for Divide Two Integers. Thank you.

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

    hats off to you sir..you deserve million subscribers.

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

    could you please make one or two videos regarding working of recursions, i.e. like generating binary strings for given no.s etc

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

    It is time to iterative approach now
    Solution based on stack
    node * reverse(node *head)
    {
    node *p, *q, *r;
    p = NULL; // we intialize pointer to reversed list
    q = head; // we initialize traversal pointer
    while(q != NULL)
    {
    r = q->next // we save next pointer in list with original order of elements
    q->next = p;
    p = q; // In these two lines we perform push operation known from stack
    q = r; // we get pointer to the next element
    }
    return p;
    }

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

    This code will not give correct output in computer. For correction We need to return head Pointer from base case then it will work.

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

    need to use a tail variable for deleting the last node from a link list :( please help

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

    Thanks a lot. Really you have the teaching capability. Any one can understand.

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

    NICE SUPER EXCELLENT MOTIVATED

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

    the return expects a node, but your return statement returns nothing..

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

    I don't understand efficiently please sir how i identify the head and null or execute

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

    May I know, Why have you stopped posting videos?

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

    You did not assign head pointer after the reverse...

  • @tnduc91
    @tnduc91 7 лет назад +3

    Appreciate your sharing, It is helpful thought your accent is a bit hard to hear!

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

    how is this a reversed linked list?You have created a loop in the linked list

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

    such a gem

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

    is this algorithm valid in c langage ???

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

    Ok, but here the return node* or q refers to NULL until you return p when q==NULL as the base case...So head is NULL which is wrong :)

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

      @@inspiritlinspirit3259
      If ( q == NULL){
      return p;
      }
      p is missing in code....

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

      We can also rewrite this function as void and pass head by a pointer

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

    Beautiful algorithm!

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

    What is the role of return after every if statement??

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

    sir you explain really well thanks a lot

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

    Thank you sir

  • @atulkumar-bb7vi
    @atulkumar-bb7vi 5 лет назад

    Why the space complexity is 1 in this problem?

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

      Because no matter how big the linked list is we always use just 2 pointers , O(2) -> O(1) . In other words the number of pointers required is constant , it doesn't grow in size when the list grows.

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

    If this was in Java it would have been easier than C. It's easier to understand that way. Pointers are difficult sometimes. Kind request is to consider to code in Java alongside C.

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

    Types of linked list explanation?????????

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

    if(q==NULL) return p;

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

    Copy a list using recursion

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

    please make on flattening of linked list video

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

    I have executed same code which is in video, Its not working.
    Below code works for sure.
    reverse(&head1);
    void reverse(struct Node **head)
    {
    struct Node *p,*q;

    if(*head == NULL)
    return ;

    p=*head;
    q=p->next;

    if(q == NULL)
    return ;

    reverse(&q);

    p->next->next=p;
    p->next=NULL;

    *head = q;
    }

  • @ashishsinha8893
    @ashishsinha8893 7 лет назад +1

    make a video without recursion sir by using temp pointer

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

    Tqqq sir

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

    superb explanation sir

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

    My solution is non recursive based on stack push operation
    void reverse(node ** head)
    {
    node *p, *q, *r;
    p = NULL; // Head of reversed list is initially set to NULL
    q = (*head); // Iterator is set to head
    while(q != NULL) // We traverse entire list
    {
    r = q->next; // We save pointer to next node
    q->next = p;
    p = q; //We push current node to the list
    q = r; //We restore saved node;
    }
    (*head) = p;
    }

  • @mohamedmahmoud-mv9dl
    @mohamedmahmoud-mv9dl 4 года назад

    this code is not working , next.next (do not do that ).
    public void ReverseList()
    {
    Node prev = null, current = head, next = null;
    while (current != null) {
    next = current.next;
    current.next = prev;
    prev = current;
    current = next;
    }
    head = prev;
    }

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

    Wrong explanation, if there is no reverse link exist how p->next->next will link in reverse order.

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

      It is correct. Imagine you have a linked list of two elements: 1 and 2. 1 points to 2 and that is your list. The reverse is 2 and 1. If you want to reverse that, you need to access 2 in order to link it to 1 so your linked list could be 2 and 1. It turns out that 2 is nothing more than 1's next since 2 is following next after 1. So 1->next = 2. 1->next->next = 2 -> next. 1->next->next = 1 => 2 -> next = 1. So now our linked list is 2, 1 where 1 follows 2 ( 2->next = 1).

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

      Try this
      ruclips.net/video/S5UByEqe8aM/видео.html

  • @275phuongvy
    @275phuongvy 3 года назад

    Great explanation ever! Thank you
    Also, can you do video on Sort List (Leetcode #148) by using Bottom Up Merge Sort method? No video uses this method to solve for Sort List.

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

      ruclips.net/video/jlHkDBEumP0/видео.html

    • @275phuongvy
      @275phuongvy 3 года назад

      @@sankalanpaulchowdhury8505 what I mentioned is Leetcode #148 using bottom up merge sort technique. Your recommended video is about top down merge sort algo.

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

    Droan clg se kon kon dekh raha hai ye video....??

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

    very good sir

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

    thanks a lot, sir

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

    love you sir.

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

    where is algorithm???

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

    Linklist polynomial addition pls..c code

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

    DS ke Ravindrababu Ravula :)

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

    circular linklist

  • @THE-or6zg
    @THE-or6zg 5 лет назад

    Sir can i get the full code of this video ??plzz sir it's urgent

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

    nice video :)

  • @ill-fatedstranger447
    @ill-fatedstranger447 6 лет назад

    Thanks

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

    Complete step wise explanation with animation. Optimised solution with single iteration.
    ruclips.net/video/txqLgAdgyVM/видео.html

  • @prasanthm7693
    @prasanthm7693 7 лет назад

    Java language seems better..#pls. kind request to continue in Java..if possible...

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

      I'm not so crazy about Java for because of garbage collector
      and i dont think that java is good language for teaching or learning programming

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

    Error in your code

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

      Try this 1st method ruclips.net/video/S5UByEqe8aM/видео.html

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

      @@SatyendraJaiswalsattu ok bro

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

      Here you will get 2 approach and you will also submit your code on hackerrank too😊

  • @SouravBiswas-hw1om
    @SouravBiswas-hw1om 6 лет назад +7

    wrong explanation

    • @nands4410
      @nands4410 6 лет назад +3

      Sourav Biswas what's wrong

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

      void push(int data,list *head)
      {
      list newNode = (list)malloc(sizeof(node));
      newNode->data = data;
      newNode->link = *head;
      *head = newNode;
      }
      //using iteration
      list reverse(list head)
      {
      list curr, prev;
      curr = head;
      head= NULL;
      while(curr!=NULL)
      {
      prev= curr;
      curr= curr->link;
      push(prev->data,&head);
      }
      return head;
      }

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

    i.e good expanation.....

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

    code is wrong. For example you can't write return without specifying variable in a non void function. I like the dude but he choked in this vid.

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

      It’s pseudo code

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

      @@jericho1211 No, code to the left is not psuedo code.

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

    This code will not work. Try it.

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

      first of all , its an algorithm explained brother not code. secondly, the algorithm seems correct.

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

    b

  • @nishantkumar8677
    @nishantkumar8677 6 лет назад +2

    time waste..not worth it

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

    bakwas vedio ..

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

    sir first you learn and then teach . Don't teach what ever you want .
    "TIME WASTE"

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

    please dont waste our time like this, Sir...

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

    poor explaination.

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

    Wrong explanation

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

    why there is not p=null in 2 step at last.