Analysis:From the definition of linked list nodes, two linked lists are unidirectional linked lists. If two one-way linked lists have public nodes, the two linked lists start from one node and Their m_pnext points to the same node.Starting from the first public node, after which all the nodes are overlapped.
For example:
Method 1:Put the nodes of the two linked lists into two stacks respectively, so that the end nodes of the two linked lists are located at the top of the two stacks, and then compare whether the nodes on the top of the two stacks are the same. If they are the same, the top of the stack will pop up and compare the top of the next stack until the last same node is found.
Space complexity: O (N1 + N2), because two auxiliary stacks are required. Time Complexity: O (N1 + N2)
Method 2:First, traverse the two linked lists to get their length. In the second time, go through several steps on the long linked list, and then traverse the two linked lists at the same time, finding the first same node is their first public node.
Time Complexity: O (N1 + N2), no additional auxiliary space required.
Code:
# Include "stdafx. H "# include <iostream >#include <stack> using namespace STD; // The node type in the linked list, struct listnode {int m_nkey; listnode * m_pnext;}; // create a linked list, input-1 to end void createlinkedlist (listnode * & pheadnode) {bool isheadnode = true; listnode * plistnode = NULL; listnode * pcurrenttail = NULL; while (1) {If (isheadnode) {pheadnode = new listnode (); CIN> pheadnode-> m_nkey; pheadnode-> m_pnext = NULL; pcurrenttail = pheadnode; Isheadnode = false;} else {plistnode = new listnode (); CIN> plistnode-> m_nkey; If (plistnode-> m_nkey =-1) {break ;} plistnode-> m_pnext = NULL; pcurrenttail-> m_pnext = plistnode; pcurrenttail = plistnode ;}}// calculate the length of the linked list, int listlength (listnode * phead) {int nlength = 0; listnode * pcur = phead; while (pcur! = NULL) {nlength ++; pcur = pcur-> m_pnext;} return nlength;} // destroy a linked table void destorylist (listnode * pheadnode) {listnode * pnode = pheadnode; listnode * pnext = NULL; while (pnode! = NULL) {pnext = pnode-> m_pnext; Delete pnode; pnode = pnext;} pheadnode = NULL; cout <"The linked list has been destroyed! "<Endl;} // print the void printlist (listnode * phead) {If (phead! = NULL) {listnode * pcurnode = phead; cout <"Print chain table from start to end:"; while (pcurnode! = NULL) {cout <pcurnode-> m_nkey <"; pcurnode = pcurnode-> m_pnext;} cout <Endl ;}} // print the linked list void print1_listreversely (listnode * phead) {stack <listnode *> tempstack; If (phead! = NULL) {listnode * pcurrent = phead; listnode * pstacknode = NULL; while (pcurrent! = NULL) {tempstack. Push (pcurrent); pcurrent = pcurrent-> m_pnext;} cout <"Print chain table from end to end:"; while (! Tempstack. empty () {pstacknode = tempstack. top (); cout <pstacknode-> m_nkey <""; tempstack. pop () ;}cout <Endl ;}// locate the first public node of the two linked lists, listnode * findfirstcommonnode (listnode * phead1, listnode * phead2) {listnode * pfirstcommonnode = NULL; int nlength1 = listlength (phead1); int nlength1 = listlength (phead2); int nlengthdiff = 0; // The difference between the two linked lists: listnode * plong = NULL; listnode * Pshort = NULL; If (nlength1> nleng22) {Protocol = nlength1-nleng2; plong = phead1; Pshort = phead2;} else {protocol = nleng22-nlength1; plong = phead2; Pshort = phead1;} int I = 0; while (I <nlengthdiff) {plong = plong-> m_pnext; I ++;} while (plong! = NULL & Pshort! = NULL & plong-> m_nkey! = Pshort-> m_nkey) {plong = plong-> m_pnext; Pshort = Pshort-> m_pnext;} pfirstcommonnode = plong; return pfirstcommonnode;} int _ tmain (INT argc, _ tchar * argv []) {// create the first listnode * phead1 = NULL; createlinkedlist (phead1); printlist (phead1 ); // create the second linked list listnode * phead2 = NULL; createlinkedlist (phead2); printlist (phead2); cout <"the first public node of the two linked lists is: "<findfirstcommonnode (phead1, phead2)-> m_nkey <Endl; // destroy two linked lists destorylist (phead1); destorylist (phead2); System (" pause "); return 0 ;}
Running result: