C language Enhancement (vii) linked list intersection problem _3 determine if a linked list is a ring

Source: Internet
Author: User

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

    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 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

Related Article

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.