Reverse a linked list - Iterative method

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ส.ค. 2024
  • See complete series on linked list here:
    • Introduction to linked...
    See complete series on data structures here:
    • Data structures
    In this lesson, we have written C program to reverse a singly linked list. This is a very famous interview question. We have solved the problem using iteration.
    Feel free to post your questions and suggestions in comments section.
    You may like us on facebook:
    / mycodeschool

ความคิดเห็น • 316

  • @mycodeschool
    @mycodeschool  11 ปีที่แล้ว +210

    We are glad that you are finding the current style useful. We will always try to keep the videos simple. So, don't worry about that.

    • @lyfokzz3848
      @lyfokzz3848 4 ปีที่แล้ว +11

      why dont you come back...
      and post some recursion related problems?
      followed by tries, huffman coding, dp n all

    • @likithmanoj3891
      @likithmanoj3891 4 ปีที่แล้ว +7

      please comeback and teach some dynamic programming and All other algos

    • @RanjanKumar-bu7ws
      @RanjanKumar-bu7ws 4 ปีที่แล้ว +3

      Please come back , come back

    • @abhishekguptasarma2060
      @abhishekguptasarma2060 4 ปีที่แล้ว +3

      ​@@lyfokzz3848 Unfortunately the creator of these videos named Humblefool passed way in a car accident in 2014. So you can't expect more videos from this channel.

    • @lyfokzz3848
      @lyfokzz3848 4 ปีที่แล้ว +8

      @@abhishekguptasarma2060 yup I knew this news...but I'm taking about his partner the one who is explaining in all these videos...
      He is doing good...and now working at Google!

  • @stevenlin2634
    @stevenlin2634 4 ปีที่แล้ว +16

    This is so far the best explanation I've ever seen to reverse a singly linked list, thanks bro!

  • @mycodeschool
    @mycodeschool  11 ปีที่แล้ว +44

    DOT is used to access the fields of an object which may be a structure type or a class type. So, if you have a structure like struct {int data; Node* next; }
    You can have Node N. and then access fields as N.data = x and N.next = y; But we use dynamic memory for node, because we want to get the memory in run time. So, there is no variable name for the node. we access it through a pointer. So, we go like Node* N = new Node(); Now, fields will be accessed as *(Node).data and *(Node).next

    • @yogipro183
      @yogipro183 2 ปีที่แล้ว +3

      Please come back man!

  • @sauravbhagat4737
    @sauravbhagat4737 6 ปีที่แล้ว +17

    The Way you proceed breaking the algorithm step by step making mistake and then correcting it in next step is awesome.

  • @armaan8449
    @armaan8449 8 ปีที่แล้ว +138

    One of the best online tutorials on Data Structures. Thank you so much for making these videos. It's really helpful to all.
    Keep up this good work!

  • @1317jack
    @1317jack 9 ปีที่แล้ว +50

    I had trouble getting my head around this, now its all clear. Excellent teaching skills indeed!
    I would love if there was a video for Directed Graphs And B-Trees

  • @mycodeschool
    @mycodeschool  11 ปีที่แล้ว +4

    Thanks Joey ! I really appreciate your feedback. I Will try to take care of this next time.

  • @quanny4690
    @quanny4690 6 ปีที่แล้ว +1

    I swear you are the best on TH-cam. Very clear and concise and with a great amount of depth

  • @RaviRanjan_ssj4
    @RaviRanjan_ssj4 9 ปีที่แล้ว +14

    This problem can't be explained more properly !! Excellent teaching skills !!

  • @sonusingh-zr6xw
    @sonusingh-zr6xw 3 ปีที่แล้ว +1

    Best explanation of reversing a linked list so far.

  • @varunn3286
    @varunn3286 5 ปีที่แล้ว +3

    I just had to hear the concept from him and could write the algorithm on my own.Please keep up the good work and continue your teaching.

  • @alxprogrmz6093
    @alxprogrmz6093 6 ปีที่แล้ว +15

    Beautiful explanation. I had a hard time figuring out how to reverse the whole link list without creating a new link list with the reverse nodes. Thank you so much. I now can complete my homework.

  • @aRoamingDuck
    @aRoamingDuck 7 ปีที่แล้ว +16

    Instantly liked the video. Finally someone that publishes their video in a proper 21:9 aspect ratio. Perfect for my Ultrawide monitor

  • @chan4est
    @chan4est 8 ปีที่แล้ว +4

    Absolutely perfect. Simple function to follow. Included implementation and testing. Good stuff!

  • @enamulhaqueshamim4439
    @enamulhaqueshamim4439 2 ปีที่แล้ว

    I am following you for the last 6 years. Still, the best with the way you describe. We are very thankfull.

  • @ardhendushekhar8417
    @ardhendushekhar8417 4 ปีที่แล้ว +1

    Thank you and please make videos like this so that everyone can understand these codes very easily.

  • @afnanreh6811
    @afnanreh6811 9 ปีที่แล้ว +5

    Thanks dude, you are doing a great job for the community. Keep it up.

  • @sethother8012
    @sethother8012 2 ปีที่แล้ว

    Wow. What a relief this video is after the last fast talking scatterbrains I gave up before here. lol Finally, someone makes some sense. This video is like a breath of fresh air

  • @Xinthose
    @Xinthose 4 ปีที่แล้ว +37

    I bombed this question in my Facebook interview, lol.

  • @merveisk1423
    @merveisk1423 5 ปีที่แล้ว +1

    You are so good at this area. You are like teachers at university. Thank you.

    • @hrithiksingh73
      @hrithiksingh73 3 ปีที่แล้ว

      100000 times better than teacher/professor at my university

  • @himanshunayak3825
    @himanshunayak3825 2 ปีที่แล้ว +1

    This is brilliant teaching. I noticed that after reversal the address for next of first node is same as head, i.e., 250 & 250. The head is ok to be 250 and next of first should be 150 which points to second node. That is first after reversal should be 5/150 instead of 5/250 at time 11:26 in video.

  • @mujahidulislam315
    @mujahidulislam315 3 ปีที่แล้ว

    Why don't you come back !!
    That the greatest you tube channel of all time for coding.

  • @joeljohnson1338
    @joeljohnson1338 4 ปีที่แล้ว +1

    Watching Lectures from here, reading from Narasimha Karumanchi 's DS and Algo made Easy and attempting same questions from Hackerrank. Enjoying it so far!

  • @tharunkumar7285
    @tharunkumar7285 6 ปีที่แล้ว

    this is best tutorial i ever watched compared to all other videos in c and even incomparable to other videos as well.Thanks alot for providing such tutorial. Iam really glad to watch this and never forget this tutorial.

  • @harshalbhite6795
    @harshalbhite6795 7 ปีที่แล้ว +2

    u're doing a great job ...really good keep going...we're with u

  • @nishantsahu8381
    @nishantsahu8381 4 ปีที่แล้ว +1

    thanks for these lectures they are really beautifully explained

  • @LamNguyen-nm1id
    @LamNguyen-nm1id ปีที่แล้ว

    it's like taking a walk in the past, very nostalgic lecture

  • @dhiaghouma929
    @dhiaghouma929 3 ปีที่แล้ว +2

    Is the head of al list a varriable containing a value and the pointer to next element in the list or is it the pointer to the first element of the list?

  • @BrettonAuerbach
    @BrettonAuerbach 9 ปีที่แล้ว +10

    @9:42 I believe it should read (*current).next since the parentheses are used because the dereference operator is lower precedence than the . operator.

    • @KaliszAd
      @KaliszAd 9 ปีที่แล้ว +2

      Bretton Auerbach I am quite sure, you are right. Semantically (current) and current ist the same. The parenthesis are there for precendence adjustion in expressions. Imagine some common arithmetics: 5 + 3 * 5 = 20 but (5 + 3) * 5 = 40. Because multiplication has higher precendence normally. If you read very carefully, you could realize, the order of my example with arithmetic operators is the same as *current.next vs (*current).next
      Also, you can read on it in Chapter 6.2 Structures and Functions on page 131 in "The C Programming Language (Second edition based on ANSI C) by Brian W. Kernighan and Dennis M. Ritchie. This book is very well written and will serve you well. Some of the style may be a bit odd and it is best used on a UNIX-like system, but it is very precise.

  • @bladerunner2912
    @bladerunner2912 4 ปีที่แล้ว

    Ohk the previous lessons concerning linked list were so great , with a bit of thinking, i reached on the iterative method by myself.
    The simplicity and clarity with which the topics are explained , if you are finding it hard to wrap your mind around this problem you should clear your basics , please visit the prev lectures

  • @mahdivs
    @mahdivs 7 ปีที่แล้ว

    Excellent teaching , excellent tutorials. One word - DS "simplified".

  • @XGRAVITYXxx
    @XGRAVITYXxx 4 ปีที่แล้ว +1

    @11:25 shouldn't the first node 250 block[5|250] actually be 150 so it points to the next node? or did i miss something and i got it wrong??

  • @rtnjo6936
    @rtnjo6936 4 ปีที่แล้ว +1

    amazing explanation! ty! hello from 2020)

  • @chilinouillesdepommesdeter819
    @chilinouillesdepommesdeter819 6 ปีที่แล้ว

    I like the way you visualise the code,great videos!

  • @RAYMONDBCE
    @RAYMONDBCE 4 ปีที่แล้ว +1

    honestly, this is better than udemy bestseller paid courses.....

  • @harpreetkaur-dp4dj
    @harpreetkaur-dp4dj 9 ปีที่แล้ว +6

    best tutorials on you tube..plz post tutorials on circular singly linked lists and circular doubly linked list

  • @eliteeliya1704
    @eliteeliya1704 4 ปีที่แล้ว +3

    source code here, i dont own this, i am just passing to those who need.
    #include
    #include
    typedef struct Node
    {
    int data;
    struct Node* next;
    }Node;
    struct Node* Reverse(struct Node* head)
    {
    struct Node *current, *prev, *next;
    current = head;
    prev = NULL;
    while (current != NULL)
    {
    next = current->next;
    current->next = prev;
    prev = current;
    current = next;
    }
    head = prev;
    return head;
    }
    Node* Insert(Node* head, int data)
    {
    Node* temp =malloc(sizeof(struct Node));
    temp->data = data; temp->next = NULL;
    if (head == NULL) head = temp;
    else {
    Node* temp1 = head;
    while (temp1->next != NULL) temp1 = temp1->next;
    temp1->next = temp;
    }
    return head;
    }
    void Print(Node* head) {
    while (head != NULL) {
    printf("%d", head->data);
    head = head->next;
    }
    printf("
    ");
    }
    int main() {
    struct Node* head = NULL; //local variable
    head = Insert(head, 2);
    head = Insert(head, 4);
    head = Insert(head, 6);
    head = Insert(head, 8);
    Print(head);
    head = Reverse(head);
    Print(head);
    }

    • @lordmoriartea7799
      @lordmoriartea7799 3 ปีที่แล้ว

      typedef struct Node
      {
      int data;
      struct Node* next;
      }Node;
      what's the use of typedef and Node; in this code?

    • @eliteeliya1704
      @eliteeliya1704 3 ปีที่แล้ว

      @@lordmoriartea7799 hello, I was using C that time. I concluded the "typedef struct Node" into "Node" as shortcut.
      For example,
      typedef struct Node
      {
      int data;
      struct Node* next;
      }NewNode;
      and we can call newnodes as NewNode everytime we need to call new one.
      In Cpp, Node def can be easily written as
      struct Node{
      int data;
      struct Node * next;
      }.

    • @lordmoriartea7799
      @lordmoriartea7799 3 ปีที่แล้ว

      @@eliteeliya1704
      //i changed the code in cpp but iam still getting errors can u please like look into it if its not much of a bother
      #include
      #include
      struct Node
      {
      int data;
      struct Node* next;
      };
      struct Node* Reverse(struct Node* head)
      {
      struct Node *current, *prev, *next;
      current = head;
      prev = NULL;
      while (current != NULL)
      {
      next = current->next;
      current->next = prev;
      prev = current;
      current = next;
      }
      head = prev;
      return head;
      }
      Node* Insert(Node* head, int data)
      {
      Node* temp =new (sizeof(struct Node));
      temp->data = data; temp->next = NULL;
      if (head == NULL) head = temp;
      else {
      Node* temp1 = head;
      while (temp1->next != NULL) temp1 = temp1->next;
      temp1->next = temp;
      }
      return head;
      }
      void Print(Node* head) {
      while (head != NULL) {
      printf("%d", head->data);
      head = head->next;
      }
      printf("
      ");
      }
      int main() {
      struct Node* head = NULL; //local variable
      head = Insert(head, 2);
      head = Insert(head, 4);
      head = Insert(head, 6);
      head = Insert(head, 8);
      Print(head);
      head = Reverse(head);
      Print(head);
      }

  • @user-cx7xo2vj8o
    @user-cx7xo2vj8o 2 ปีที่แล้ว

    Best explanation to reverse a linked list... Thanks so much.

  • @yannai1705
    @yannai1705 7 ปีที่แล้ว +1

    thank you very much!! i had hw with linked lists and i had to reverse them but and i knew i
    should use a recursive function but i didn't know how to imagine this and then you showed how

  • @ankitbhardwaj9566
    @ankitbhardwaj9566 5 ปีที่แล้ว

    REALLY CLEARED ALL MY DOUBTS ON LINKED LIST TILL NOW

  • @meghapaul1496
    @meghapaul1496 3 ปีที่แล้ว

    This is one of the best explanation on reversing a Linked list ❤️

  • @ironhidesatz
    @ironhidesatz 10 ปีที่แล้ว +4

    Hi Thanks for the tutorials, they are awesome !!
    Please can you explain the insert () function which accepts head and data as arguments.
    at the end you return head , but how is the head manipulated in the "else" of the function ?
    thanks

  • @vnnmichael
    @vnnmichael 11 ปีที่แล้ว +1

    Please Sir....I learn only through repitition and revising some basic concepts as we move towards advanced concepts....Knowledge and patience go hand in hand....please do not change your style of teaching jus because some one cant sit thru repitition

  • @vnnmichael
    @vnnmichael 11 ปีที่แล้ว

    The videos are great. Especially because there is lot of repitition !!!! I dont think repitition is waste of tiime...I think repitition is a way of learning . Those who do not have patience to sit through some repititive stuff ,can jus read thru a book where it is easy fr them to skip things. This professor who ever is making these lectures is doing a great job. hats off

    • @inayatvohra8550
      @inayatvohra8550 2 ปีที่แล้ว

      1up81800e+/&&-(9📝_:+)) -_₹+8-'gu .

    • @inayatvohra8550
      @inayatvohra8550 2 ปีที่แล้ว

      Whisww bu[yyy fg r555 good crfxc xcf3orjjhr

  • @sharmavikas2
    @sharmavikas2 11 ปีที่แล้ว

    I am sorry but I beg to differ with you Joey. You may be an expert but novice like me require and love the reiteration of previous material as it solidifies the concept and helps a lot in relating things. For building a strong fundamentals ,it`s really an effective technique....

  • @hand__banana
    @hand__banana 3 ปีที่แล้ว +2

    rest in peace, thank you for the knowledge

  • @mycodeschool
    @mycodeschool  11 ปีที่แล้ว

    1st and 3rd are not correct.... When you put '*' operator in front of a variable, you are trying to access the value at the address stored in that variable. So, if temp is pointer to node, you can only access the fields of node as *(temp).data or (*temp).next..... if you say temp = head , you are modifying the address in temp.. Third one is incorrect because you need to use parentheses and say *(temp).prev.... because of operator precedence issue. Second one is correct.

  • @mannambhavani4266
    @mannambhavani4266 4 ปีที่แล้ว

    That was great sir! Except for the thing that for beginners its lil hard to catch your exceptional flow, rest everything is soooooo good sir. Helpful thanks a lot.

  • @venkatesh3439
    @venkatesh3439 4 ปีที่แล้ว

    you lectures are best of all!!!

  • @GameSteals
    @GameSteals 7 ปีที่แล้ว

    I saw another technique in Sumita Arora. It said there, that we can create a newHead node and start inserting elements of the given linked list to the newHead node and return newHead. This reverses the linked list with newHead being the starting node.

  • @LetsPlayClipAll
    @LetsPlayClipAll 5 ปีที่แล้ว

    From Only this tutorial I could understand the reverse logic of linked list in iterative manner mind blow explanation. I really liked your teaching efforts.

    • @jaffarjawed4407
      @jaffarjawed4407 4 ปีที่แล้ว

      th-cam.com/video/dR7v1sVX428/w-d-xo.html

  • @suryaprakashn5994
    @suryaprakashn5994 3 ปีที่แล้ว

    @mycodeschool Thankyou so much for this amazing video.

  • @srisoftengg
    @srisoftengg 4 ปีที่แล้ว

    simple and nice but valuable 13 minutes in my life Thanks

  • @sanjitjena4954
    @sanjitjena4954 ปีที่แล้ว

    The video was good enough (rather, GREAT 👍)
    You can improve the explanation by separating the canvas into heap area and stack area, so that the pointer vars are not confused with nodes.
    also, you can reuse the head pointer in place of using an extra next pointer.

  • @siddharthc6364
    @siddharthc6364 2 ปีที่แล้ว

    At 11.25 of the video, the first node should contain address of the next node as 150, not 250. Great content bro and ur voice is soothing.

  • @togrulnbili8703
    @togrulnbili8703 6 ปีที่แล้ว

    Really smooth and understandable.You are amazing.Thank you

  • @davidgaitsgory712
    @davidgaitsgory712 9 ปีที่แล้ว

    I''ve watched all your explanations on linked lists. Thank you so much for these!

  • @thestarinthesky_
    @thestarinthesky_ 4 ปีที่แล้ว +3

    @9:45, the statement *(current).next won't work in C++; instead, we need to write (*current).next!

    • @DemondKing
      @DemondKing 4 ปีที่แล้ว +1

      or do current->next

  • @zainabmerchant4378
    @zainabmerchant4378 5 ปีที่แล้ว +1

    Can you please make a video on generalised linked list for multivariable polynomial representation ?

  • @Young10489
    @Young10489 3 ปีที่แล้ว

    invaluable contents sir! really hit the nail right on the head. thanks so much

  • @utkarshpant5297
    @utkarshpant5297 4 ปีที่แล้ว +25

    Finally I know how to reverse a Linked list and get rid of that girl

  • @aliazizi8770
    @aliazizi8770 5 ปีที่แล้ว +1

    Your video was so helpful
    It also works in Java
    Thanks!!!

    • @bipinjaiswal5654
      @bipinjaiswal5654 5 ปีที่แล้ว

      Hi, i see this implementation is explained in c c++ , not sure but if you are from java background how are you able to connect it or how iy helping you to connect with java

  • @debajyotidatta8480
    @debajyotidatta8480 ปีที่แล้ว +1

    code:
    #include
    using namespace std;
    struct node
    {
    int data;
    node *next;
    };
    node *head;
    void display(){
    node *temp=head;
    while(temp!=0){
    coutnext;
    current->next=prev;
    prev=current;
    current=next;
    }
    head= prev;
    }
    int main()
    {
    head=0;
    insert(1);
    insert(2);
    insert(3);
    insert(4);
    insert(5);
    display();
    reverse();
    display();
    return 0;
    }

  • @killabyt3
    @killabyt3 ปีที่แล้ว

    Thank you this helped me understand better

  • @shashankshekharismd
    @shashankshekharismd 11 ปีที่แล้ว

    *(current).next same as current->next , i always thought "." is used for variables other than pointers. This means we can always use
    *(temp)=head
    *(temp).next=*(head)->next
    *temp.prev=head->next
    Right? I mean these all are correct syntax!

  • @surfmoment
    @surfmoment 7 ปีที่แล้ว +7

    thanks' my friend. the best lesson of my life :)

  • @parthdevbundela6744
    @parthdevbundela6744 2 ปีที่แล้ว

    NODE* deletenth(node* head,int n)
    this returns a new/desired value of head

  • @dailygyansks
    @dailygyansks 3 ปีที่แล้ว

    In the insert function,why head is used as an argument while not done so in the previous videos. Please explain any one........

  • @Akanksha8618
    @Akanksha8618 8 ปีที่แล้ว

    Perfect illustration!

  • @mahammedhashish9170
    @mahammedhashish9170 2 ปีที่แล้ว

    Very Good explanation, Thankyou

  • @mohitahlawat4944
    @mohitahlawat4944 4 ปีที่แล้ว

    great video Si..r you always put it in the
    best way

  • @keithprice7119
    @keithprice7119 3 ปีที่แล้ว

    Thanks for the very clear explanation.

  • @ankitrajput85
    @ankitrajput85 6 ปีที่แล้ว

    Perfectly explained! Great video. Thank you!

  • @mohitsaraswat8504
    @mohitsaraswat8504 6 ปีที่แล้ว

    Sir very good explanation helped a lot for my exam

  • @kishoreprabhu3523
    @kishoreprabhu3523 5 ปีที่แล้ว

    Really appreciatable for this tutorial

  • @insofcury
    @insofcury 4 ปีที่แล้ว

    Thank you for the beautiful explanation

  • @patrickfreeman1112
    @patrickfreeman1112 2 ปีที่แล้ว

    I'm still having a hard time understanding why you are initializing *next again and the difference between the two Node pointers both named next at 11:10

  • @seraj_valley
    @seraj_valley 5 ปีที่แล้ว +1

    Every time head will change??? In if statement

  • @ankitaa.baruah3970
    @ankitaa.baruah3970 9 ปีที่แล้ว +1

    I really liked this video. Thank you so much!

  • @rhuthuhegde3353
    @rhuthuhegde3353 4 ปีที่แล้ว +1

    Okay this was a great explanation to reverse a ll.
    But to keep things very simple can't we use" inserting the node at the beginning of the node".?? It also gives the same result and we don't have to break our heads too :)

    • @inayatvohra8550
      @inayatvohra8550 2 ปีที่แล้ว

      Ekatwht Shri thunder aasbia0t233pweona8geeew0s

  • @mukeshpathak7302
    @mukeshpathak7302 8 ปีที่แล้ว +2

    i want to start make tutorials on programming. can you please tell me how you make these videos? what type of board is that you using to explain..a real one or virtual one?

  • @akankshamishra7941
    @akankshamishra7941 3 ปีที่แล้ว

    Wonderful explanation

  • @abhinav4095
    @abhinav4095 4 ปีที่แล้ว

    what is head node?does it contain data or just a memory address

  • @sharatmalkhed7543
    @sharatmalkhed7543 6 ปีที่แล้ว

    comparision between the time complexity in iterative and recuression methods....

  • @tobytran2775
    @tobytran2775 6 ปีที่แล้ว

    Really helpful video!! This is incredibly amazing!!! Thanks so much.

  • @mkmk418
    @mkmk418 9 ปีที่แล้ว

    Nice, very clear and useful. Thanks!

  • @jae766
    @jae766 2 ปีที่แล้ว +1

    이 동영상으로 인해 더이상 구글링을 하지 않아도 되었습니다. 정말 고맙습니다.

  • @mohitshrivastava1158
    @mohitshrivastava1158 7 ปีที่แล้ว

    Great work all points covered

  • @shivakumar-lv6wr
    @shivakumar-lv6wr 6 ปีที่แล้ว

    Very nice!. Ek Baar mein samajh aagaya

  • @vijayank3207
    @vijayank3207 8 ปีที่แล้ว

    THANK YOU SO MUCH SIR.....nice teaching......

  • @KishoreKumar-sl6fk
    @KishoreKumar-sl6fk 5 ปีที่แล้ว

    Could you tell us about time and space complexity of these programs... may not be added in the video but in the comments or description section... it wud b great if you could add those...

  • @satyarthvimal2271
    @satyarthvimal2271 2 ปีที่แล้ว

    Best explanation

  • @twentyeightO1
    @twentyeightO1 3 ปีที่แล้ว

    Pure gold! Thank you.

  • @sglbl
    @sglbl 4 ปีที่แล้ว +2

    What do i miss in the code?It shows only 2
    #include
    #include
    struct Node{
    int data;
    struct Node* next;
    };
    typedef struct Node Node;
    struct Node* head;
    Node* Insert(Node* head,int data){
    Node *temp = (struct Node*)malloc(sizeof(struct Node));
    temp -> data= data;
    temp->next=NULL;
    if(head==NULL) head=temp;
    else{
    Node* temp1 = head;
    while(temp1->next != NULL) temp1->next = temp;
    }
    return head;

    }
    void Print(Node *head){
    while(head !=NULL){
    printf("%d ",head->data);
    head =head->next;
    }
    if(head==NULL){
    printf("head=nullmuş.");
    }
    }
    struct Node *Reverse(struct Node *head){
    struct Node *current,*prev,*next;
    current = head;
    prev = NULL;
    while(current!=NULL)
    {
    next = current -> next;
    current -> next = prev;
    prev = current;
    current = next;
    }
    head=prev;
    return head;

    }
    int main(){
    struct Node *head =NULL; //local variable
    head = Insert(head,2); //Insert: struct Node* Insert(struct Node* head,int data)
    head = Insert(head,5);
    head = Insert(head,6);
    head = Insert(head,8);
    Print(head);
    head = Reverse(head);
    Print(head);
    }

  • @GagandeepSingh-tl7zg
    @GagandeepSingh-tl7zg 4 ปีที่แล้ว +1

    Java implementation continues...
    public Node reverse() {
    // Can not traverse empty list.
    if (size < 1) {
    throw new RuntimeException("Can't reverse empty list.");
    }
    // We don't need to make any changes to the linked list as this is always same even after reverse.
    if (size == 1) {
    return head;
    }
    Node previous = null;
    Node current = head.next;
    Node next = null;
    while (current != null) {
    next = current.next;
    current.next = previous;
    previous = current;
    current = next;
    }
    head.next = previous;
    return head;
    }
    Have made a slight change to print method too:
    public void print() {
    print(head);
    }
    public void print(Node head) {
    Node address = head;
    if (address.next == null) {
    System.out.println("Empty");
    return;
    }
    while (address.next != null) {
    System.out.println(address.next.data);
    address = address.next;
    }
    }

  • @ArunKumar-wj4yj
    @ArunKumar-wj4yj 9 ปีที่แล้ว

    Thanks a lot. Nice tutorial for beginners.

  • @sonalijadhav9555
    @sonalijadhav9555 4 ปีที่แล้ว

    Thank you so much for great knowledge🙏🙏

  • @suyash.01
    @suyash.01 3 ปีที่แล้ว

    Very well explained!

  • @joycewang5019
    @joycewang5019 5 ปีที่แล้ว

    This tutorial is very helpful for me, thank you :D

  • @nareshpadala4005
    @nareshpadala4005 9 ปีที่แล้ว

    thank u very much sir. This video removes lots of confusion in mind.

    • @HeshamSalehVEVO
      @HeshamSalehVEVO 8 ปีที่แล้ว

      +Naresh Padala Gorrrd! Very exlant.