How to tell if a linked list intersects

Source: Internet
Author: User

Linked lists intersect: For linked lists, two linked lists are merged into one successor if they intersect, followed by a linked list. According to the theory of intersection, two simple linked lists can be set up to intersect.

When the user enters the 10 point in the first list as the point to intersect, the second list enters 0 and intersects with the 10 point of the first linked list.

The code is as follows:

1#include <stdio.h>2typedefstructlinklist{3     intnum;4     structLinklist *Next;5 }list;6 7 structLinklist *inch;8 9 voidAddlink (List *head) {TenList *NewNode; One     intNumber=5; A      while(1){ -printf"Please input:"); -scanf"%d",&Number ); the         if(number==0){ -head->next=NULL; -             return; -         } +Newnode= (structLinklist *)malloc(sizeof(structlinklist)); -newnode->num=Number ; +head->next=NewNode; AHead=NewNode; at         if(number==Ten){ -             inch=head; -         } -     } -head->next=NULL; - } in  - voidADDLINK2 (List *head) { toList *newnode,*p; + //P=head; -     intNumber=5; the      while(number!=0){ *printf"Please input:"); $scanf"%d",&Number );Panax Notoginseng         if(number==0){ -newnode->next=inch; the             return; +         }     ANewnode= (structLinklist *)malloc(sizeof(structlinklist)); thenewnode->num=Number ; +head->next=NewNode; -Head=NewNode; $     } $      - } -  the voidPrintlink (List *head) { -List *p;WuyiP=head->Next; the      while(1){ -printf"%d,",p->num); WuHead=p; -P=p->Next; About         if(P==null)return; $     } - } -  -  A  +  the intMainvoid){ -     structLinklist *head= (structLinklist *)malloc(sizeof(structlinklist)); $     structLinklist *head2= (structLinklist *)malloc(sizeof(structlinklist)); the      the Addlink (head); the Printlink (head); theprintf"\ n"); -      in addLink2 (head2); the Printlink (head2); theprintf"\ n"); About      the     return 0;  the}

This establishes a linked list of intersections and outputs:

This implements the intersection of the linked list.

Then is to solve the problem of the intersection of the linked list, then how to determine the intersection of the linked list?

See how the two linked lists intersect in the end, there are several facts about this : (Assuming that there is no ring in the list)

(1) Once the two linked lists intersect, then the nodes in the two linked list must have the same address.

(2) Once the two linked lists intersect, the two linked list must be the same node from the intersection node to the tail node.

I think of the method similar to the selection of sorting, from the second list to select an element and each of the first linked list of each element of the subsequent point of the address as a comparison, the occurrence of the same address element, it means that the linked list has intersect.

namely A->next==b->next

The second method is to look at the last element of each list, and if the address of the last element of the two list is the same, then the list must intersect.

So if the amount of data is very large, is the amount of data negotiated, so that the traversal is very slow, two lists compiled once is the square, this time should be how to judge it?

Then there is a solution is: You can put the second linked list after the first linked list, if the resulting list has a ring, then the two linked lists Intersect, otherwise, the two linked lists do not intersect, so that the problem to determine whether a linked list has a ring, so the use of the detection loop may be faster.

How to tell if a linked list intersects

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.