C Language Enhancement (7) linked list intersection question _ 3 determine whether a linked list has a ring and intersection _ 3

Source: Internet
Author: User

C Language Enhancement (7) linked list intersection question _ 3 determine whether a linked list has a ring and intersection _ 3

The premise of the previous two discussions is that the linked list has no loops, but what if the linked list has loops?

Obviously, if the linked list has a ring, the function of searching for the End Node of the linked list will be in an endless loop, and the previous algorithm will also collapse.

To solve the intersection problem of the linked list, we must first determine whether the linked list has a ring.


Question

Two head pointers of one-way linked list, such as h1 and h2, are given to determine whether the two linked lists are intersecting.


Solution steps

  1. Determine whether two [no-ring] linked lists are intersecting
  2. Locate the intersection nodes of two [no-ring] linked lists
  3. Judge whether the linked list has a ring
  4. Determine whether two [having rings] linked lists overlap
  5. Locate the intersection nodes of two [having rings] linked lists
IdeasUse two pointers, one pointer step is 1, and the other pointer step is 2. If the last encounter occurs, the linked list has a ring.
Returns the intersection of two pointers with loops.
NULL is returned if no loops exist.
Function used to determine whether a linked list has a ring:
/** Method for determining whether the linked list has a node linked list head pointer: Use two pointers. one pointer step is 1 and the other is 2, if the linked list has loops and loops, 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; // The step size is 2 slow = slow-> nextNode; // The step size is 1 If (fast = slow) {cout <"linked list with loops" <endl; return fast ;}} cout <"linked list without loops" <endl; return NULL ;}

Source code
# Include <stdio. h> # include <stdlib. h> # include <iostream> using namespace std;/** 3. how to determine whether a linked list has a ring: Use two pointers, one pointer step is 1, and the other pointer step is 2, if the linked list has loops and loops, return NULL when the two pointers meet each other. * // returns the structure of the linked list */struct ListNode {int data; ListNode * nextNode; ListNode (ListNode * node, int value) {nextNode = node; data = value ;}}; ListNode * L1;/** determine whether the linked list has a link node linked list header pointer. Method: Use two pointers, one pointer step is 1, and one pointer step is 2. If the last encounter occurs, the linked list has a ring and returns the position where the two pointers meet. If no ring exists, the return value is 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; // The step size is 2 slow = slow-> nextNode; // The step size is 1 If (fast = slow) {cout <"linked list with loops" <endl; return fast ;}} cout <"linked list without loops" <endl; return NULL ;} // create the ListNode * createList () {ListNode * node = new ListNode (NULL, 0); node = new ListNode (node, 1); node = new ListNode (node, 2); return node;} // create a ListNode * createCircleList () {ListNode * node = new ListNode (NULL, 0); ListNode * enter = node; n Ode = 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 <"the encounter node of the two pointers is" <L1-> data <endl; system ("pause ");}

Now, we can determine whether the linked list has a ring. If there is no ring, determine whether or not the linked list is intersecting and obtain the intersection node according to the method described in the previous two sections. What if there is a ring? Next lecture.

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.