Linked List crossover

Source: Internet
Author: User

A URL points to another URL in the page, and a URL points to a previous URL or empty. Both cases are defined as null. This constitutes a single-chain table. For two such single-chain tables, determine whether the same URL exists. The URL is measured in hundreds of millions, and the resources are insufficient for hash.

If yes, the URL of the last node must be the same!

No matter how the linked list is linked, whether or not the linked list A and linked list B have nodes or only the content of nodes in the actual implementation, the last nodes of the two linked lists must be the same, if it is repeated!

We need to examine the questions carefully and consider the questions carefully. It is best to draw a picture so that we will not be confused.

Once a linked list problem occurs, you must consider the problems of loops and intersections;

A URL points to another URL in the page, and a URL points to a previous URL or empty. Both cases are defined as null. This constitutes a single-chain table. For two such single-chain tables, determine whether the same URL exists. The URL is measured in hundreds of millions, and the resources are insufficient for hash.

This question can be abstracted as a linked list crossover problem with loops and no loops:

 

Scenario 1:Two single-chain tables have no rings

In the simplest case, because if two linked lists overlap, their tail nodes must be equal (Y word merge), so you only need to judge whether their tail nodes are equal.

 

Scenario 2:Two single-chain tables have Loops

In this case, you only need to split a loop (note that you need to save the node that is set to null), and then judge whether another single-chain table still has a loop. If yes, there is no crossover. Otherwise, there is a crossover.

 

Case 3:Two single-chain tables, one with a loop and the other without a loop

Obviously, they cannot overlap.

 

Appendix:How to determine whether a single-chain table has a loop and find the loop entry

Speed pointer: set two pointers in the header: fast and slow. The fast pointer and slow pointer move forward at the same time. However, fast moves two nodes at a time, and slow moves one node at a time, if fast points to null or fast = slow, then if fast points to null, there is no loop. If fast = slow, there is a loop.

Find loop entry: When fast = slow, point fast to the header again. Slow remains unchanged. Then fast and slow move forward at the same time at the speed of each node. When they re-match, it is the loop entry. The proof is as follows:

1. Prove that fast and slow will surely overlap

When slow and fast met for the first time, it was assumed that slow took n steps and the loop entrance was completed during step P, so there was a path for slow: P + c = N; C is the intersection of P1 and P2, distance from the loop entry; fast path: P + C + K * l = 2 * n; l is the circumference of the loop, and K is an integer. Obviously, if P1 takes n steps again from P + C and P + C, you can go back to P + C and start from p2 at the same time. Then, after N steps, it will also reach P + C.

2. fast and slow will overlap at the P + C point. Obviously, they begin to overlap from the entry point of the ring.

Http://iam42.iteye.com/blog/1680444

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.