The understanding of the recursive algorithm of linked list algorithms

Source: Internet
Author: User

A: Preface

Today in the blog Park to read an article http://www.cnblogs.com/huangxincheng/p/4051854.html (single-linked list upside down), in fact, they looked at the last one hours or not to see clearly, their own do not understand that, Recursive invocation to the last execution of the recursive code is how to execute, if executed, where the data from the execution? I just don't understand that, but I can read this code myself. At this time, I do not know whether or not to count. Let's talk about your understanding first!!!

Two: I copied his piece of code, a single-linked list myself and casually write a:9---->6------->8------>2

           Publiclinknode Reverse (Linknode node) {if(Node.next = =NULL)                  returnnode;//This is the time of the loop to 3, which means that when the node.next=2 is calledReverse (*) method, the result of the last return is ' 2 ', back to the execution of the recursive method below the//Three pieces of code, at this time the data how come I'm not very clear.              varPrevNode =Reverse (Node.next); vartemp =Node.next; Temp.next=node; Node.next=NULL; returnPrevNode; }                

After returning ' 2 ', then Var prenode=2, then the var temp =node.next, at this time node=2,2 's next is empty, empty next point to node, then the next node is empty, then continue to node=8,8 the next would have been ' 2 ', that is, at this time temp equals ' 2 ', temp.next=node at this point ' 2 ' the next point to ' 8 ', and then ' 8 ' the next null. And so on, still think here is a piece of code good coincidence Ah!!! I hope my understanding is correct.

Three: I did a little test to help understand

 Packagelink; Public classLinkdemo { Public Static voidMain (String args[]) {printf (0); }     Public Static intprintfinti) {        if(i==10){            returni; }Else{i=i+1; //System.out.println (Value of "I" +i);printf (i);//If this is written in this way printf (i++), the value of each pass is I, but did not add 1, has been carried out, and then package a mistake, can be tested, but if it becomes ++i, then it can beSystem.out.println ("+i+" "secondary---->" +i); returni; }    }}

Four: summary

I used to look at the algorithm when I always feel that it is difficult to understand, today, the next test of their own, just know the need to understand in practice, more than their own only read a better AH. Later still need to learn more algorithm, too clever ah. Hope these days can be summed up the knowledge, a lot of ah, there is no time to tidy up, I also want to go to some of the source of research. Only try to squeeze the time!!!

The understanding of the recursive algorithm of linked list algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.