List of the last K nodes in a list

Source: Internet
Author: User

Topic
Enter a list to output the last K nodes in the linked list.

Analysis
For this problem, consider the single-linked list implementation, single-linked list can only traverse from beginning to end, and to find the bottom of the K-node, it is necessary to determine that the positive number is the first node, assuming that the total node is n, the last node position is n-1, and the reciprocal K-node position is n-k+1, Just traverse to n-k+1, which means we need to know two key information, one is the link list length, one is n-k+1, this needs to traverse two times, obviously, this is not the best solution. The better way is to be able to find a single traversal, assuming we use two pointers, when one pointer is pointing to n, the other pointer is k-1 from him, the other pointer points to the position of n-k+1, which is the position we're looking for, It is an effective way to implement this problem with two pointers with K-1.

"Test Code"

#include<Stdio.H>#include<Stdlib.H>#include<Stack>typedef int DATA_TYPE;TYPEDEF struct Node node_t;//Assign individual names to struct node node_ttypedef struct NODE*Node_ptr;//Give the struct node* an individual name node_ptrtypedef struct node{Data_typeData; struct Node*Node_next;//node_next is a pointer to a struct that tells the pointer to address it to a struct-type address};//List initializationnode_t*Init () {node_ptr p; P=(node_t*) malloc (sizeof (node_t)); P -Node_next= NULL;returnP;}//Insert a node behind a linked listnode_t*Insert_back (Node_ptr p, data_typeData) {Node_ptr pnew=(node_t*) malloc (sizeof (node_t)); Pnew -Node_next= NULL; Pnew -Data = Data; P -Node_next=Pnew;returnPnew;} node_t*Find (node_ptr p, unsigned int k) {if(p== NULL ||K== 0)return NULL; Node_ptr Pahead=P Node_ptr Pbehind=NULL; for (int i= 0; I<K-1; I++)  {if(Pahead -Node_next!= NULL) Pahead=Pahead -Node_next;Else          return NULL; } pbehind=P while(Pahead -Node_next!= NULL) {Pahead=Pahead -Node_next; Pbehind=Pbehind -Node_next; }returnPbehind;}//Normal printingvoidPrint (Node_ptr p) {if(!P) {printf ("No data, you think too much");return; } while(p -Node_next!= NULL) {printf ("%d"P -Data); P=P -Node_next; } printf ("%d"P -Data); printf"\ n");}voidMain () {node_ptr pnode,List; Pnode=Init ();List =Pnode; Pnode=Insert_back (Pnode,1); Pnode=Insert_back (Pnode,2); Pnode=Insert_back (Pnode,3); Pnode=Insert_back (Pnode,4); Pnode=Insert_back (Pnode,5); Pnode=Insert_back (Pnode,6); node_t*Target=FindList,3); int Find_data=Target -Data; printf"The 3rd Count is:%d\n", find_data);}

Here must pay attention to some special conditions, if the single-linked list is empty, the location of the lookup is set to 0, the total number of nodes is less than k, these are to be considered, otherwise, once the test problem occurs, the entire program will crash, small loopholes can make the whole fall short.

Output

Extension
This problem thinking can be extended to other need to traverse the test, such as testing a single-linked list is not a loopback, set two pointers, one pointer at a time, the other two steps, to the end if the fast pointer unexpectedly catch up with the slow that is the loop, If you walk quickly to the end of the list or not catch the slow instructions, not the loopback; and the test single-linked list of intermediate nodes, set two pointers, one step, the other two steps, walk two steps to the end, walk slowly to the middle node, because the fast pointer is twice times slower, quickly went to the n-1 position, The slow position is (n-1)/2, a pointer that cannot be traversed once to solve the problem with two pointers.

List of the last K nodes in a list

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.