C language Enhancement (vii) linked list intersection problem _4 determine if two linked lists Intersect

Source: Internet
Author: User

At the end of the previous section, we can already determine if the list has a ring, if there is no ring, then according to the first two sections of the method to determine whether the linked list intersect and get intersect nodes, if there is a ring it? How do you tell if you intersect?


Topic

Give the head pointers of two unidirectional lists, such as H1,H2, to determine if the two lists intersect


Problem solving steps

    1. Determine if two "ring-free" linked lists intersect
    2. Found intersection nodes of two "ring-free" linked lists
    3. Determine if a linked list has a ring
    4. Determine if two "ring" linked lists intersect
    5. Found intersection nodes of two "ring" linked lists
Ideasfor linked lists with loops, as long as they intersect, the section with the ring must be completely duplicated. So we just need to find a node on the link in the chain list, to determine if the node is on the list two, if it is, then intersect, if not, then not intersect. So how do you find a node on the link on the list?? Yes, the last one tells you whether the linked list has a ring function, and if the chain band ring, it returns a node on the ring!
based on the idea above, we need to create two new functions function One: Determine if the node is on a linked list.
/* Determine if the node is not on the linked list  head-linked header node node  */bool ifnodeonlist (listnode * head,listnode * node) {if (node==null) return 0;// To prevent an infinite traversal of a linked list, the first is to have a ring-listnode * Circlenode = ifcircle (head); int count = 0;//The number of repeated nodes while (Head!=null&&count <2) {if (Head==node) return 1;if (Head==circlenode) Count++;head=head->nextnode;} return 0;}

function Two: Determine if a linked list intersects
Determine if a linked list intersects bool Ifcirclelistcross (ListNode * L1,listnode * L2) {ListNode * node = ifcircle (L1); if (Node!=null) return Ifnodeonlist (l2,node); return 0;}


Source code:
#include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std;/**4. Judging two "rings" Whether the linked list crosses the line of thought to determine the node that the two pointers meet on a linked list, "on the other linked list". If in, then intersect, if not, then not intersect. *//** linked list structure */struct listnode{int data; ListNode * NEXTNODE; ListNode (ListNode * node,int value) {nextnode=node;data=value;}}; ListNode * L1; ListNode * l2;/** to determine if a linked list has a ring node-linked table Head pointer method: With two pointers, a pointer step of 1, a pointer step of 2, if the last encounter, then the chain list has a ring return two pointers to meet the position of the loop return Null*/listnode * Ifcircle ( ListNode * node) {if (Null==node) return false; ListNode * Fast = node->nextnode; ListNode * slow = Node;while (null!=fast&&null!=fast->nextnode) {fast=fast->nextnode->nextnode;// Step is 2slow=slow->nextnode;//step is 1if (fast==slow) {cout<< "linked list has ring" <<endl;return fast;}} cout<< "linked list no ring" <<endl;return NULL;} /* Determine if the node is not on the linked list head-linked header node node */bool ifnodeonlist (ListNode * head,listnode * node) {if (node==null) return 0;//to prevent an infinite traversal of the linked list, First, there is no ring to judge ListNode * Circlenode = ifcircle (head); int count = 0;//The number of repeated nodes while (HEAD!=NULL&AMP;&AMP;COUNT&LT;2) {if ( Head==node) return 1;if (HEad==circlenode) Count++;head=head->nextnode;} return 0;} Determine if a linked list intersects bool Ifcirclelistcross (ListNode * L1,listnode * L2) {ListNode * node = ifcircle (L1); if (Node!=null) return Ifnodeonlist (l2,node); return 0;} Create a linked list ListNode * Createcirclelist () {ListNode * node = new ListNode (null,0); ListNode * Enter = Node;node = new ListNode (node,1); node = new ListNode (node,2); enter->nextnode=node;node = new Listnod E (node,3); node = new ListNode (node,4); return node;} Create a ring-linked list that intersects void Createcirclelistcross () {L1 = new ListNode (null,0); ListNode * Enter2 = L1;//L2 's Entrance L1 = new ListNode (l1,1); L1 = new ListNode (l1,2); enter2->nextnode=l1;//l1 's Entrance L1 = new ListNode (l1,3); L1 = new ListNode (l1,4); L2 = new ListNode (enter2,0); L2 = new ListNode (l2,1); L2 = new ListNode (l2,2);} Create a ring linked list disjoint void Createcirclelistnotcross () {l1=createcirclelist (); L2=createcirclelist ();} void Main () {/*listnode * node = createcirclelist (); ListNode * Node2 = new ListNode (null,0); Cout<<ifnodeonlist (Node,node2) <<endl;*///createcircleliStnotcross (); Createcirclelistcross (); if (Ifcirclelistcross (L1,L2)) cout<< "intersect" <<endl;elsecout<< "Disjoint" <<endl;system ("pause");}

whether the linked list intersects we can also judge, the rest is to get the link table intersection node, the next section, linked list intersection problem of the last section, chat.

C language Enhancement (vii) linked list intersection problem _4 determine if two linked lists Intersect

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.