Step-by-step write algorithm (linked list coincident)

Source: Internet
Author: User

Original: Step by step write algorithm (the linked list coincident)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Linked list coincidence is a fun question. The original topic is this: there are two linked lists, then how to determine whether the two linked lists are not coincident? As to when the list is coincident, this is not important, the key is to determine whether the list is not coincident. What is the method?

The simplest way is to see if they have anything in common. Then you can judge it in turn.

int Find_node_in_link (link_node* pLink, link_node* pnode) {while (PLink) {if (PLink = = Pnode) return 1;plink = plink-> Next;} return 0;} STATUS Find_if_link_merge (link_node* plinkone, link_node* plinktwo) {link_node* phead;if (NULL = = Plinkone | | NULL = = plinktwo) return false;phead = Plinktwo;while (phead) {if (Find_node_in_link (Plinkone, Phead)) return true;phead = Phead->next;} return FALSE;}
Another method is the method of counting, since the list is coincident somewhere, then the number of visits to this point is 2, so we can go through two linked lists in turn, and finally see if there is no node count is 2.

typedef struct _LINK_NODE{INT data;int count;struct _link_node* Next;} Link_node;void Process_all_link_node (link_node* pnode) {assert (NULL! = Pnode), while (Pnode) {pnode->count + +;p NODE = Pnode->next;}}
From the counting method, we can find that if the two linked lists are coincident, then their last node is necessarily the same, so just determine if the last node is the same.

STATUS Find_if_link_merge (link_node* plinkone, link_node* plinktwo) {assert (NULL! = Plinkone && NULL! = plinktwo) while (plinkone->next) Plinkone = Plinkone->next;while (plinktwo->next) plinktwo = Plinktwo->next;return ( Plinkone = = plinktwo)? True:false;}

Summarize:

1) The list of coincident topics, although simple, but from different angles can have different answers;

2) This topic from the "beauty of programming", if the solution is also interested in Friends can refer to the "beauty of programming."

Step-by-step write algorithm (linked list coincident)

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.