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)