C language Enhancement (vii) the intersection of linked list _5 found two intersecting nodes of a linked list

Source: Internet
Author: User

If the linked list intersects we can also judge, the rest is to get the linked list intersection node


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

Ideas:Obviously, the intersection point of a linked list is actually the entrance to the ring.

So the question turns to the entrance to the ring.

Directly on the theory, the specific explanation is not difficult, is purely a primary school math catch-up problem

If the head node and the fast and fast hands meet the node to set a pointer, synchronous (single Step) forward, then must meet in the ring entrance junction

For the introduction of fast and fast pointers, please refer to the third section of linked list intersection


Create a function: Get a linked list entry

/* Get a linked list entry if you set a pointer at the head node and the encounter node, and synchronize (single step) forward, then you must meet at the ring entrance node */listnode * Getcirclelistenter (ListNode * head) {if (head==null). return NULL; ListNode * node = ifcircle (head), if (node==null) return Null;while (node!=null&&head!=null) {if (node==head) return node;node=node->nextnode;head=head->nextnode;} return NULL;}

Source

#include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std;/**5. Found two "rings" The idea of the intersection node of the linked list is to find two entry points if the head node and the encounter node set a pointer, synchronous (single Step) forward, then must meet in the ring entrance junction *//** 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; 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;} /* Get a linked list entry if you set a pointer at the head node and the encounter node, and synchronize (single step) forward, then you must meet at the ring entrance node */listnode * Getcirclelistenter (ListNode * head) {if (head==null). return NULL; ListNode * node = ifcircle (head), if (node==null) return Null;while (node!=null&&head!=null) {if (node==head) return node;node=node->nextnode;head=head->nextnode;} return NULL;} 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 () {Createcirclelistcross (); ListNode * node = getcirclelistenter (L1);cout<< "Portal with linked list L1 (intersection point 1)" <<node->data<<endl;node = Getcirclelistenter (L2);cout<< "The entrance (intersection point 2) of the" linked list L2 "<<node->data<<endl;system (" pause ");}

at this point, the ancient chain of the intersection of the problem finally finished, summed up the idea

Determine if a linked list is with a ring (section III)

Without ring, the method of non-ring, to determine whether the intersection, the intersection point (section I, section II)

Band ring, with a ring method, to determine whether to intersect, the intersection point (section III, section Fourth)


C language Enhancement (vii) the intersection of linked list _5 found two intersecting nodes of a linked list

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.