"The sword refers to the offer" question 22nd (the list of the penultimate K nodes) and 23rd mention (the entry point of the linked list)

Source: Internet
Author: User
Tags printf

Question 22nd: The Countdown K node in the list

Topic:

Enter a list to output the last K nodes in the list. In order to conform to most people's habit, the subject starts from 1, that is, the tail node of the list is the last node of the countdown. For example, a linked list has 6 nodes, starting from the beginning, and its values are 1, 2, 3, 4, 5, 6, respectively. The bottom 3rd node of the list is a node with a value of 4. The linked list is defined as follows:

struct ListNode
{
    int m_nvale;
    listnode* m_pnext;
};

The problem solving procedure is as follows:

#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std;
    struct ListNode {int m_nvale;
listnode* M_pnext;

};
    Create a node for the list ListNode *createlistnode (int val) {ListNode * pnew = (listnode*) malloc (sizeof (ListNode)); 
        if (pnew = = NULL) {printf ("pnew malloc failed!\n");
    return NULL;
    } pnew->m_pnext = NULL;
    Pnew->m_nvale = val;
return pnew; }//Connection list void Connectlistnode (listnode* pnode1,listnode* pNode2) {if (PNode1 = = NULL) {printf ("PNode1
        is null\n ");
    Return
} pnode1->m_pnext = PNode2;
        }//Destroy the list void Destroylistnode (listnode* phead) {while (phead) {ListNode *temp = Phead;
        
        Phead = phead->m_pnext;
        Free (temp);
    temp = NULL;
        }}//Print List void Printlistnode (ListNode *pnode) {while (Pnode) {printf ("%d\t", Pnode->m_nvale);
    Pnode = pnode->m_pnext;
} printf ("\ n");
}
Find the number of K nodes in the list ListNode *findkthtotail (ListNode *phead,int k) {if (Phead = = NULL | | k<=0) return NULL;    listnode* pahead = Phead;    The first traversed pointer listnode* pbehind = NULL; The second traversal pointer//Pahead Walk K-1 step for (int i =0;i<k-1;i++) {//loop traversal, you should determine if the next node is empty if (pahead-
        >m_pnext = NULL) {Pahead = pahead->m_pnext;    
        }//If the next node is empty and has not traversed the k-1 step, it will go wrong and return null else {return null;
    }}//give pbehind pointer to the head of the list, traverse Pbehind = Phead from the beginning; 
        Then Pahead,pbehind go together until the Pbehind pointer goes to the tail of the list while (pahead->m_pnext) {pbehind = pbehind->m_pnext;
    Pahead = pahead->m_pnext;
} return pbehind; 
   }//testing case void Test () {ListNode *pnode1 = Createlistnode (1); 
   ListNode *pnode2 = Createlistnode (2); 
   ListNode *pnode3 = Createlistnode (3); 
   ListNode *pnode4 = Createlistnode (4); ListNode *PNODE5 = creatElistnode (5); 

    ListNode *pnode6 = Createlistnode (6);
    Connectlistnode (PNODE1,PNODE2);
    Connectlistnode (PNODE2,PNODE3);
    Connectlistnode (PNODE3,PNODE4);
    Connectlistnode (PNODE4,PNODE5);

    Connectlistnode (PNODE5,PNODE6);

    Printlistnode (PNODE1);
    int num = 0;
    
    printf ("Please enter the list of the link to find the first pour of K nodes: \ n");
        
    scanf ("%d", &num); 
        while (num<=0) {printf ("Your input is wrong, please re-enter: \ n");
    scanf ("%d", &num);
    } ListNode *temp = Findkthtotail (pnode1,num);
    if (temp! = NULL) {printf ("Pour the value of the K node:%d\n", Temp->m_nvale);
    } else {printf ("The list%d node does not exist \ n", num);
} destroylistnode (PNODE1);
    } int main (void) {test ();
return 0; }
Question 23rd: The entry node of the ring in the linked list

Topic:

If a linked list contains loops, how to find the entry node of the ring. For example, the entry node for the ring in the linked list as shown in the following figure is 3.


Problem Solving Program:

#include <stdio.h> #include <stdlib.h> using namespace std;
    struct ListNode {int m_nvale;
listnode* M_pnext;

};
    Create a node for the list ListNode *createlistnode (int val) {ListNode * pnew = (listnode*) malloc (sizeof (ListNode)); 
        if (pnew = = NULL) {printf ("pnew malloc failed!\n");
    return NULL;
    } pnew->m_pnext = NULL;
    Pnew->m_nvale = val;
return pnew; }//Connection list void Connectlistnode (listnode* pnode1,listnode* pNode2) {if (PNode1 = = NULL) {printf ("PNode1
        is null\n ");
    Return
} pnode1->m_pnext = PNode2;
        }//Print List void Printlistnode (ListNode *pnode) {while (Pnode) {printf ("%d\t", Pnode->m_nvale);
    Pnode = pnode->m_pnext;
} printf ("\ n");

    }//Find the Node listnode* meetingnode (listnode* phead) {if (Phead = null) When the fast pointer encounters the return null;       ListNode *pslow = phead->m_pnext;

  Fast pointer//check if the linked list has only one node if (Pslow = = null) return null;  ListNode *pfast = pslow->m_pnext; Slow pointer//loop over find the node that meets the knot while (pfast! = NULL && Pslow! = null) {//Pfast = = Pslow means two pointers meet,

        Find the Encounter node if (pfast = = Pslow) return pfast;
        Pslow = pslow->m_pnext;

        Pfast = pfast->m_pnext; Prevents Pfast to the end of the list, causing a program error//fast pointer to walk two steps each time, a slow pointer every step of the way if (pfast! = NULL) {pfast = Pfast->m 
        _pnext;
}}//did not find the encounter node, that is, the list does not exist ring return NULL; }//Look for the number of nodes in the ring, and then find the entry point ListNode *entrynodeofloop (ListNode *phead) {//Find two pointers met node ListNode *meetingnode = Mee 
    
    Tingnode (Phead); 
    
    if (null = = Meetingnode) return NULL;
    Gets the number of nodes in the ring int nodesinloop = 1;
    
    ListNode *pnode1 = Meetingnode;
        Loop traversal, when traversing to the next node and the encounter node is the same, you can find the number of nodes in the ring while (pnode1->m_pnext! = Meetingnode) {nodesinloop++;
    PNode1 = pnode1->m_pnext;
    }//From the head of the list to start traversing pNode1 = Phead; From the head of the linked list to theAfter traversing nodesinloop each node for (int i = 0;i<nodesinloop; i++) {pNode1 = pnode1->m_pnext;
    } listnode* pNode2 = Phead; 
        Start looping through two pointers, knowing that the nodes that the two pointers point to are the same as the entry node of the link of the linked list while (pNode1! = pNode2) {pNode1 = pnode1->m_pnext;
    PNode2 = pnode2->m_pnext;
} return pNode1; 

    }//Destroy the linked list void Destroylistnode (listnode* phead) {//Get chain List of the ring of the meat listnode* Pnodeofloop = Entrynodeofloop (Phead);
        If it is a ring, and the entry node of the ring is known, the method of destroying the ring list is as follows if (NULL! = pnodeofloop) {ListNode *findnodeoftail = Phead;
        while (findnodeoftail->m_pnext! = pnodeofloop) {Findnodeoftail = findnodeoftail->m_pnext;
    }//Pointer to the last node, pointing to null, not pointing to the ring Findnodeoftail->m_pnext = null;
        } while (Phead) {ListNode *temp = Phead;

        Phead = phead->m_pnext;
        Free (temp);
    temp = NULL;
}}//testing case void Test () {//Create linked list node ListNode *pnode1 = Createlistnode (1);    ListNode *pnode2 = Createlistnode (2);
    ListNode *pnode3 = Createlistnode (3);
    ListNode *pnode4 = Createlistnode (4);
    ListNode *PNODE5 = Createlistnode (5);

    ListNode *pnode6 = Createlistnode (6);
    Link List junction connectlistnode (Pnode1,pnode2);
    Connectlistnode (PNODE2,PNODE3);
    Connectlistnode (PNODE3,PNODE4);
    Connectlistnode (PNODE4,PNODE5);

    Connectlistnode (PNODE5,PNODE6);
   
    Print linked list node printlistnode (PNODE1);

    Make the chain list exist ring Connectlistnode (PNODE6,PNODE3);
    Gets the entry node of the ring ListNode *temp = Entrynodeofloop (pNode1);
    if (NULL = = temp) {printf ("link list does not exist Ring!\n");
    } else {printf ("The entry node of the ring is:%d\n", Temp->m_nvale);

}//Destroy linked list Destroylistnode (PNODE1); 
    } int main (void) {test ();
return 0; }

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.