Sword refers to offer-, chapter III, High-quality code (outputs the penultimate K-node in the list)

Source: Internet
Author: User

Title: Enter a linked list and output the countdown k node in the list. (Robustness of the code)

Idea: With two pointers P1 and P2, all pointing to the head node, at the beginning, p2 not move, p1 move k-1 times, point to the K node. At this point, if P1->next!=null, the P1 and P2 are moved at the same time. Until P1 points to the last node. At this point, the P2 points to the bottom K node.

C + + code:

#include <iostream>using namespacestd;structlistnode{intM_nvalue; ListNode*M_pnext;}; ListNode* CreateList (intA[],intk) {ListNode* phead=null,*pnode=NULL;  for(intI=0; i<k;i++) {ListNode* pnew=NewListNode (); Pnew->m_nvalue=A[i]; Pnew->m_pnext=NULL; if(phead==NULL) {Phead=pnew; Pnode=pnew; }        Else{Pnode->m_pnext=pnew; Pnode=pnode->M_pnext; }    }    returnPhead;}voidPrintlist (listnode*phead) {ListNode* p=Phead;  while(p!=NULL) {cout<<p->m_nValue<<" "; P=p->M_pnext; } cout<<Endl;} ListNode* Printktotail (listnode* phead,unsignedintk) {    if(phead==null| | k==0)        returnNULL; ListNode* pahead=Phead; ListNode* ptail=NULL;  for(intI=0; i<k-1; i++)    {        if(pahead->m_pnext!=NULL) Pahead=pahead->M_pnext; Else            returnNULL; } ptail=Phead;  while(pahead->m_pnext!=NULL) {Pahead=pahead->M_pnext; Ptail=ptail->M_pnext; }    returnPtail;}voidMain () {inta[]={1,2,3,4,5}; ListNode* Phead=createlist (A,5);    Printlist (Phead); ListNode* Pktail=printktotail (Phead,2); cout<<pKTail->m_nValue<<Endl;}

Java code: The embodiment of robustness: In this function Printktotail, the first head pointer and the K value to judge, the for statement with the IF statement on the total length of less than the value of the time when the judgment.

 Public classPrintktotail { Public  Static classListNode { Public intM_nvalue;  PublicListNode M_pnext; }    //Create a linked list     Public  StaticListNode Createlink (intA[],intk) {ListNode Head=NULL, q=NULL;  for(inti=0;i<k;i++) {ListNode pnew=NewListNode (); Pnew.m_nvalue=A[i]; Pnew.m_pnext=NULL; if(head==NULL) {Head=pnew; Q=pnew; }              Else{Q.m_pnext=pnew; Q=Q.m_pnext; }          }          returnHead; }      //Print List from beginning to end     Public Static  voidPrintlink (ListNode phead) {ListNode P=Phead;  while(P! =NULL) {System.out.print (P.m_nvalue+" "); P=P.m_pnext; } System.out.println ("\ n"); }      //the reciprocal k nodes of the output and output list     Public StaticListNode Printktotail (ListNode phead,intk) {if(phead==NULL|| K<=0)            return NULL; ListNode Pahead=Phead; ListNode Ptail=NULL;  for(inti=0;i<k-1;i++)        {            if(pahead.m_pnext!=NULL) Pahead=Pahead.m_pnext; Else                return NULL; } ptail=Phead;  while(pahead.m_pnext!=NULL) {Pahead=Pahead.m_pnext; Ptail=Ptail.m_pnext; }        returnPtail; }     Public Static voidMain (string[] args) {inta[]={1,2,3}; ListNode Head=createlink (a,3);          Printlink (Head); ListNode Pktail=printktotail (head,2); System.out.println (Pktail.m_nvalue+" "); }     }

Related Topic 1: Find the middle node of the linked list.

Idea: The same is the definition of two pointers P1 and P2, both to move the pointer at the same time, just p1 the pointer each move one step, the P2 pointer each move two steps, when the P2 point to the tail of the list, P1 just point to the middle node.

Related Topic 2: Determine whether a one-way linked list is annular.

Idea: The same definition of two P1 and P2, two pointers moving simultaneously, when P2 can catch up to the P1, indicating that the unidirectional link list is annular. Otherwise, when the P2 has gone to the tail, and P1 has not yet caught up with P2. On behalf of the change of one-way linked list is not annular.

Sword refers to offer-, chapter III, High-quality code (outputs the penultimate K-node in the 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.