C Language Enhancement (7) linked list intersection question _ 5 find the intersection nodes of two link chains, _ 5 nodes

Source: Internet
Author: User

C Language Enhancement (7) linked list intersection question _ 5 find the intersection nodes of two link chains, _ 5 nodes

We can also determine whether a link table has an intersection. The rest is to obtain the intersection node of the Link Table.


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


Ideas:Obviously, the intersection of a chain table is actually the entrance of the ring.

So Entry for converting a problem into a ring

It is not difficult to explain the theory directly. It is simply a catch-up problem in primary school mathematics.

If a pointer is set at the first knot and the fast and slow pointer encounter respectively, and the synchronization (single step) advances, the last encounter must be at the ring entry node.

For more information about speed pointers, see section 3 of linked list Intersection


Create a function: Obtain the entry for a chain table.

/* Obtain the entry of a chain table. If a pointer is set at the first node and the encounter node, the table is synchronized (in a single step, in the end, it must meet at the entry 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 code

# Include <stdio. h> # include <stdlib. h> # include <iostream> using namespace std;/** 5. find the intersection nodes of the two [having rings] linked lists. The idea is to find two entry points. If one pointer is set at the first node and the encounter node, synchronize the two points (one step, in the end, it must meet at the ring entry node * // ** linked list structure */struct ListNode {int data; ListNode * nextNode; ListNode (ListNode * node, int value) {nextNode = node; data = value ;}}; ListNode * L1; ListNode * L2;/** determine whether the linked list has a ring 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 (ListNod E * 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 ;} /* determine whether the node is in the head linked list head node */bool ifNodeOnList (ListNode * head, ListNode * node) {if (node = NULL) return 0; // to prevent infinite traversal of a chain table, first judge ListNode * circleNode = ifCircle (head); int count = 0; // The number of times the node has been repeated while (head! = NULL & count <2) {if (head = node) return 1; if (head = circleNode) count ++; head = head-> nextNode ;} return 0;} // determine whether a chain table exists in the bool ifCircleListCross (ListNode * L1, ListNode * L2) {ListNode * node = ifCircle (L1); if (node! = NULL) return ifNodeOnList (L2, node); return 0;}/* Get the entry to the chain table. If a pointer is set at the first node and the encounter node, the synchronization (single step) advances, in the end, it must meet at the entry 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 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;} // create a void createCircleListCross () {L1 = new ListNode (NULL, 0); ListNode * enter2 = L1; // L2 entry L1 = new ListNode (L1, 1); L1 = new ListNode (L1, 2); enter2-> nextNode = L1; // L1 entry 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 void createCircleListNotCross () {L1 = createCircleList () with an unmatched link chain table (); l2 = createCircleList ();} void main () {createCircleListCross (); ListNode * node = getCircleListEnter (L1); cout <"entry of L1 with link chain table (intersection 1) "<node-> data <endl; node = getCircleListEnter (L2); cout <" entry with L2 Link Table (intersection 2) "<node-> data <endl; system (" pause ");}

So far, this old linked list intersection problem is finally finished, and the following ideas are summarized.

Judge whether the linked list has a ring (Section 3)

Without a ring, use the method of without a ring to determine whether or not the intersection is obtained (Section 1 and section 2)

With a ring, use the method of a ring to determine whether or not the intersection and intersection (section 3 and section 4)


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.