The premise of the first two discussions is that the list is non-ring, but what if the list has a ring?
Obviously, if the list has a ring, then the previous function looking for the tail node of the chain will fall into a dead loop, and the previous algorithm will collapse.
so for the intersection of linked lists, the first thing to determine is whether the linked list has a ring .
Topic
Give the head pointers of two unidirectional lists, such as H1,H2, to determine if the two lists intersect
Problem solving steps
- Determine if two "ring-free" linked lists intersect
- Found intersection nodes of two "ring-free" linked lists
- Determine if a linked list has a ring
- Determine if two "ring" linked lists intersect
- Found intersection nodes of two "ring" linked lists
Ideas with two pointers, a pointer step of 1, a pointer step of 2, if the last encounter, the linked list has a ring
There's a loop back where the two pointers meet.
No loop returns null
to determine whether a linked list has a ring function:
/** to determine whether the linked list has a ring node link 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 loop 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;}
Source Code
#include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std;/**3. To determine whether the chain table with a ring thinking 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 loop return two pointers to meet the position of the loop return null*//** linked list structure */struct listnode{int data; ListNode * NEXTNODE; ListNode (ListNode * node,int value) {nextnode=node;data=value;}}; ListNode * l1;/** 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;} Create a loop-free list ListNode * CreateList () {ListNode * node = new ListNode (null,0); node = new ListNode (node,1); node = new ListNode (nod e,2); return node;} 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 ListNode (node,3); node = new ListNode (node,4); return node;} void Main () {//l1=createlist (); L1=createcirclelist (); L1=ifcircle (L1); if (l1!=null) cout<< "Two pointers Meet node" <<l1->data<<endl;system ("pause");}
at this point, we can already determine whether the linked list has a ring, if there is no ring, then according to the previous two sections of the method to determine whether the linked list intersect and get intersect nodes, if there is a ring it? Next talk, talk.
C language Enhancement (vii) linked list intersection problem _3 determine if a linked list is a ring