Note the typical example of robustness and algorithmic efficiency: (header file omitted)
typedefstructnode{intdata; structnode*Next;} ListNode; ListNode* Findkthtotail (listnode* plisthead, unsignedintk);intMain () {inti; Const intN = -; ListNode*head, *p; //random number seedSrand ((unsignedint) Time (0)); //Generate linked listHead = P =NewListNode; Head->data = rand ()%101; Head->next =nullptr; for(i =0; i < N; i++) {p= P->next =NewListNode; P->data = rand ()%101; P->next =nullptr; } //Output Link List for(p = head; p; p = p->next) {cout<<" "<< p->data; } cout<<Endl; //Generate random numbersi = rand ()%101; cout<<"Random number n="<< I <<Endl; //number of output Countdown Kp = findkthtotail (head,3); if(nullptr = =p) {cout<<"Find Error"<<Endl; } Else{cout<<"Find Result:"<< P->data <<Endl; } cin.Get(); return 0;} ListNode* Findkthtotail (listnode* plisthead, unsignedintk) { if(nullptr = = Plisthead | |!k)returnnullptr; inti; ListNode*p, *Q; P= q =Plisthead; for(i =0; I < K1; i++) { if(nullptr==p->next) { returnnullptr; } Else{p= p->Next; } } while(nullptr! = p->next) {P= p->Next; Q= q->Next; } returnq;}
List of the penultimate K nodes in the list