Daily question (11) -- Single-linked list FAQ

Source: Internet
Author: User
1. Single-chain table access 1. find the last k elements of a single-chain table (only the linked list can be traversed) two pointers fast, slow: Fast first advances to the K position; then the two pointers move together, until fast reaches the end, the element referred to by slow is desired. 2. find the intermediate element of a single-chain table (only one linked list can be traversed) two pointers fast, slow: fast move twice each time, slow move once each time until fast reaches the end, the element referred to by slow is the request. All Use Pointer catch-up methods. Ii. Single-chain table and ring Problems 1. Determine whether a single-chain table has a ring (6 shapes )? Still use the pointer catch-up method: two pointers fast, slow: Starting from the pointer (starting point), fast moves two times after each time, slow moves one time after each time, if met, the two have loops; if fast encounters null, it exits (no ring exists, so fast encounters null) 2. how can I find the change entry? When fast and slow meet at the collision point, the distance that slow walks through is (a + x), and the distance that fast walks through is (a + 2x + Y ), the forward distance of fast is twice that of slow. Therefore, y = A can be obtained. Therefore, the two slow pointers start from the starting point and the collision point respectively, and the encounter point is the ring entry. 3. How to know the length of the ring? Start from the collision point and reach the collision point again. The distance traveled is the cycle length S. 4. What is the length of the linked list with loops? L = a + S; 3. Single-chain table and intersection and ring Problems

 

Linked List intersection:

1. How do I know whether two single-chain tables (without loops) are mutually exclusive?

Solution 1:

Whether or not each element of the first linked list is in the second node, the time O (L1 * l2 );

Solution 2:

If the two linked lists intersect, the two linked lists have a common node. The Node Address is the unique identifier of the node. If we can determine whether two linked lists have nodes with the same address, we can know whether the two linked lists are intersecting.

Hash Sorting is performed on the node addresses in the first linked list to create a hash table. The address of each node in the second linked list queries the hash table. If the hash table is displayed, it is public to the same node. Time O (l1 + l2) Additional O (L1) storage space.

 

Both methods are not satisfactory.

How do I know whether two single-chain tables (without loops) overlap?

(1) Determine whether the last node of the two linked lists is the same. If the two nodes overlap, the end node must be the same node. Time complexity O (length (A) + Length (B), space complexity = O (1) (2 ). the artificial ring points the tail node of linked list A to Linked List B. If the Linked List B has a ring, the two linked lists will intersect. At this time, the first pointer of linked list B will traverse down. If it can return to B, it indicates the intersection time complexity O (length (A) + Length (B), no additional space consumption (isn't it BT ~ After a is connected to B, B starts to rotate ~ That's because the end of a is actually the end of B ~ )

 

2. If two single-chain tables (without loops) overlap, how can we know what the first node of their intersection is?

Or catch up with pointers:

(1) first, get the length Len (A) and Len (B) (2) of two linked lists A and B, and traverse long linked lists in the and B linked lists, use the pointer catch-up method. Set two pointers: fast and slow. Fast first sets forward (lengthmax-lengthmin) Step (that is, the difference in the length of the two) to make the distance between the fast and slow pointer to the intersection point equal, then the two linked lists move forward at the same time, each step, the first point of an encounter is the first point of the intersection of two linked lists. (PS: This method is brainless ~)

 

3. How do I know if two single-chain tables (with loops) are mutually exclusive?

Solution 1: Find the ring entrance of a and determine whether it is on the B linked list. If it is on the B linked list, it will intersection.

Solution 2: On the linked list, use the pointer catch-up method to locate the two pointer collision points, and then determine whether the collision points are on the B linked list. If yes

 

4. If two single-chain tables (with rings) overlap, how do I know what the first node of the two single-chain tables is?

(Note: The intersection of two chain tables indicates that the ring belongs to two linked lists)

Case 1: intersection point, out-of-the-loop approach: Use the pointer catch-up method. Evaluate the length of two linked lists A and B: length (A) and length (B). If length (a)> length (B), then the linked list a pointer first length () -length (B): the pointer B of the linked list starts to go. The intersection of the two pointers is the first node of the intersection. If length (a) <length (B), the pointer B of the linked list first goes through length (B)-length (A), and the pointer A of the linked list begins to go, the intersection of the two pointers is the first node of the intersection. Case 2: intersection point, in the ring, cannot be processed

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.