C + + algorithm to find out two public nodes of a list

Source: Internet
Author: User

Title: Enter two linked lists to find their first public node. The linked list node is defined as follows:

struct ListNode

int M_nkey;

listnode* M_pnext;

Method 1: Sequentially iterates through each node on the first list, does not traverse one node, and sequentially iterates through each node on the second linked list. O (n^2)

Method 2: Find the length of the two linked lists, first traverse the long list to the length of the short linked list, and then two linked lists at the same time, do not traverse to compare two node pointer is the same,

Note that the node pointer is compared, not the value of the node!

Code:

 

FindFirstCommandNode.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;struct listnode{int M_nkey; listnode* M_pnext; ListNode (int i): M_nkey (i) {}};//Gets the chain table length int getlistlength (listnode* phead) {int nlength = 0; listnode* Pnode = Phead;while (pnode! = NULL) {++nlength;pnode = Pnode->m_pnext;} return nlength;} listnode* Findfirstcommandnode (listnode* pHead1, listnode* pHead2) {int nLength1 = getlistlength (pHead1); int nLength2 = G Etlistlength (pHead2); int nlengthdif = 0;//Two list length difference listnode* Plistheadlong = null;//used to point to long list listnode* Plistheadshort = null;//is used to point to short lists//depending on length to determine the list of links to if (NLength1 > nLength2) {nlengthdif = Nlength1-nlength2;plistheadshort = Phead2;plisth Eadlong = PHead1;} Else{nlengthdif = Nlength2-nlength1;plistheadlong = Phead2;plistheadshort = PHead1;} Move the long linked list first to the same position as the short list length for (int i = 0; i < nlengthdif; i++) {plistheadlong = Plistheadlong->m_pnext;} Looking for common nodes while (Plistheadlong!=null && plistheadshort! = NULL && plistheadlong!= plistheadshort) {Plistheadlong = Plistheadlong->m_pnext;plistheadshort = pListHeadShort- >m_pnext;} If the Plistheadlong is not empty at this time, the node with Plistnodeshort is returned as the same node if (Plistheadlong! = NULL) {return plistheadlong;} Else{return null;//otherwise returns null}}int _tmain (int argc, _tchar* argv[]) {listnode* head1 = new ListNode (0); listnode* head2 = new ListNode (1); listnode* node0 = new ListNode (22); listnode* Node1 = new ListNode (2); listnode* Node2 = new ListNode (3); listnode* node3 = new ListNode (4); listnode* node4 = new ListNode (5); listnode* node5 = new ListNode (6); listnode* node6 = new ListNode (7); listnode* node8 = new ListNode (6); head1->m_pnext = Node1;node1->m_pnext = Node0;node0->m_pnext = node3;node3-& Gt;m_pnext = Node5;node5->m_pnext = Node6;node6->m_pnext = Null;head2->m_pnext = Node2;node2->m_pNext = Node4;node4->m_pnext = Node8;node8->m_pnext = Node6;node6->m_pnext = null;cout<< "The length of the list 1 is:" << Getlistlength (HEAD1) <<endl;cout<< "chainThe length of table 2 is: "<<getlistlength (head2) <<endl; listnode* Commnode = Findfirstcommandnode (head1,head2), if (commnode!= NULL) {cout<< "The value of the public node is:" <<commnode- >m_nkey<<endl;} else{cout<< "No public Node" &LT;&LT;ENDL;} GetChar (); return 0;}

C + + algorithm to find out two public nodes of a list

Related Article

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.