C language Enhancement (vii) the intersection problem of linked list _2 find the junction of the non-ring linked list

Source: Internet
Author: User

I talked to you in the last section. if the two "ring-free" linked lists intersect, how do you find the Intersect node if you intersect?


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
    • The length of the linked list is recorded during traversal L1 and L2 (assuming L1>L2)
    • Traverse to find the L1-L2 node in the first linked list,
    • The list of links begins with the first L1-L2 node, List two is traversed from the first node, which is equivalent to two linked lists starting at the same time as two locations at the same distance from the intersection .
    • Each time before further
    • Until the first identical node is found, you can assume that the two linked lists have intersect nodes.
    • The point is the first intersecting node
thought diagram
Source Code
#include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std;/**2. Find two "no rings" The intersection of the linked list of ideas traverse the process of recording the length of the linked list L1 and L2 (assuming L1&GT;L2) then traverse to find the first linked list in the L1-L2 node, and then the chain table one from the L1-L2 node to start traversing, the list of two from the first node traversal, each time before further, Until the first identical node is found, the two linked lists can be considered to have intersecting nodes, and that point is the first intersecting node *//** the linked list structure */struct listnode{int data; ListNode * NEXTNODE; ListNode (ListNode * node,int value) {nextnode=node;data=value;}}; ListNode * L1; ListNode * l2;/** Get the list length */int getlistlength (ListNode * head) {int i =0;if (head==null) return 0;while (head->nextnode!= NULL) {head=head->nextnode;i++;} return i;} /** Gets the linked table node at the specified location */listnode * Getthatlistnode (ListNode * head,int pos) {int i =0;if (head==null) return Null;while (head-> Nextnode!=null) {head=head->nextnode;i++;if (pos==i) return head;} return NULL;} /** get two non-linked list intersection node L1: longer linked list L2: Short list */listnode * Getnocirclelistcrossnode (ListNode * L1,listnode * L2) {ListNode * l_long; ListNode * L_short;int Start;int length1 = getlistlength (L1); int length2 = Getlistlength (L2); if (length1>=length2) {L_ LONG=L1; l_short=l2;start=Length1-length2;} ELSE{L_LONG=L2; L_short=l1;start=length2-length1;} L_long=getthatlistnode (L_long,start); while (L_long->nextnode!=null&&l_short->nextnode!=null) {if (L _long==l_short) return l_long; l_long=l_long->nextnode; L_short=l_short->nextnode;} return NULL;} Test loop-free intersect void Testcross () {//Intersect segment ListNode * node = new ListNode (null,0); node = new ListNode (node,1); node = new ListNode (node, 2); node = new ListNode (node,3);//start intersection here L1 = new ListNode (node,11); L2 = new ListNode (node,21);//disjoint segment L1 = new ListNode (l1,12); L1 = new ListNode (l1,13); L2 = new ListNode (l2,22); L2 = new ListNode (l2,23); L2 = new ListNode (l2,24); L2 = new ListNode (l2,25);} void Main () {Testcross ();//int length1 = Getlistlength (L1);//cout<<length1<<endl;//listnode * node = Getthatlistnode (L1, 3);//cout<<node->data<<endl; ListNode * node = Getnocirclelistcrossnode (L1,L2); if (node!=null) cout<<node->data<<endl;elsecout< < "No intersect point" <<endl;system ("pause");}

the premise of the first two discussions is that the list is non-ring, but what if the list has a ring? Next article, chat.

C language Enhancement (vii) the intersection problem of linked list _2 find the junction of the non-ring 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.