One-way list of data structures and algorithms two: get the K-node of the penultimate page

Source: Internet
Author: User

When we do the algorithm, we are more or less confronted with the problem that we need to get the number of the reciprocal or positive numbers of a data set. So let's take a look at this question today and how to get to the bottom K node. When we get this problem, we naturally think that we let the list start at the end of the next K-1 times is not the first K-1 node, but must note that this is a one-way list. Then the solution to the idea may be divided, most people will think of us to traverse the list, to get the length of the list, and then subtract the K-length of the node, then we are the last node of the list is the original linked list K node: We look at the implementation of the Code:

/*** Get data from the last K nodes *@paramIndex *@return     */     Public intGetdtae (intindex) {        //traverse the entire list        intSize = 0; Node Current= head;//Head is the first node.         while(current!=NULL) {size++; Current=Current.next; } Current=Head; //traverse the size-k backwards to get the K-node of the penultimate page         for(inti = 0;i < size-index;i++) { current=Current.next; }        returncurrent.date; }

We can see that this code can implement the functions we need, of course. So the question is, if we enter the index greater than the length of the list or the list itself is an empty list, then we will not have this code problem. Or when our index equals 0, there will be problems, normally we will count from the countdown to the first, the penultimate 0 is not meaningless, then this piece of code is not strong enough. This problem maybe we have a little bit of good programming thinking that we will stay until the final solution. Here's what we need to think about is how to get the data above while traversing a linked list. Can we define two nodes first and second, and they point to the head node at the same time. We move the second node backward index-1 step, when first and second is not the distance between K, we put two nodes back at the same time, when the second reached the end of the list, it is not possible to say that the first position is we need the countdown K node. The code is as follows:

/*** Get data from the last K nodes *@paramIndex *@return     */     Public intGetdtae (intindex) {        //define two nodes to point to headNode first =Head; Node Second=Head; //move the second node backwards k-1 step         for(inti = 0;i < index-1;i++) {Second=Second.next; }        //move the two nodes backwards at the same time until the second reaches the end position.         while(second!=NULL) { First=First.next; Second=Second.next; }        returnfirst.date; }

We can see if this piece of code is already implemented. But the problem is that the strength of a piece of code lies in its ability to handle special events, not to crash the entire program. Next we do the following to avoid the three problems we say, index equals 0,index more than the length of the list, the list is empty list of the case:

/*** Get data from the last K nodes *@paramIndex *@return     */     Public intGetdtae (intindex) {        //determines whether index is zero or is less than 0 illegal data        if(Index <= 0){            //throw null pointer exception            Throw NewNullPointerException (); }        //define two nodes to point to headNode first =Head; Node Second=Head; //The second node moves backwards K-1 step         for(inti = 0;i < index-1;i++){            //determine if the second is empty            if(second==NULL){                Throw NewNullPointerException (); } Second=Second.next; }        //two nodes move backwards until the end of the linked list         while(second!=NULL) { First=First.next; Second=Second.next; }        returnfirst.date; }

We can see that at the beginning of the direct judgment K equals 0, we directly judge whether he is empty when the second node moves backwards, if the linked list is empty, then the first second is naturally empty, if index is greater than the list length, in the subsequent next process, Natural second can also produce empty conditions. This is the perfect solution to the above mentioned three cases, index equals zero, index is greater than the chain list length, the list is empty case. When we are testing, we can insert data into a linked list through my last blog, and then perform functional tests to get three nodes of data from the end of the list, and in the case of special tests you can enter the three kinds of cases to test.

One-way list of data structures and algorithms two: get the K-node of the penultimate page

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.