Determines whether two linked lists are intersecting. searches for the first public node of the two linked lists.

Source: Internet
Author: User

Problem 1: (the beauty of referencing programming) If two linked lists overlap, the end node must be public

Question 2:

1 int listlength (node * List) {2 int length = 0; 3 while (list! = NULL) {4 length ++; 5 List = List-> next; 6} 7 return length; 8} 9 // 10 node * firstcomnode (node * list1, node * list2) {11 int n1 = listlength (list1); 12 INT n2 = listlength (list2); 13 // defines the pointer plong, Pshort, point to the long list and short list 14 node * plong = list1; 15 node * Pshort = list2; 16 if (N1 <N2) {17 plong = list2; 18 Pshort = list1; 19} 20 // move the pointer to the long list a certain distance to D21 int d = (n1-n2)> 0 )? (N1-n2) :( n2-n1); 22 while (d --) {23 plong = plong-> next; 24} 25 // two pointers traverse to the tail at the same time, find the first same node 26 while (plong! = NULL & plong! = Pshort) {27 plong = plong-> next; 28 Pshort = Pshort-> next; 29} 30 return plong; 31}

 

Determines whether two linked lists are intersecting. searches for the first public node of the two linked lists.

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.