Linked List Problems

Source: Internet
Author: User

I collected a list of common questions:

1. How to judge whether a single-chain table has a ring

2. How to determine the entry point of a ring

3. How to know the ring Length

4. How do I know whether two single-chain tables (with no rings) overlap?

5. How do I know the first node of a single-chain table (without loops)

6. How do I know whether two single-chain tables (with rings) overlap?

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

1. Use the speed step method. Let the two pointers p and q point to the header node respectively. P points to each forward step, and Q points to each forward step. If p and q can overlap, a ring exists. It can be understood that this approach is equivalent to P still, Q every step forward, all certainly catch up with P.

We noticed that the pointers p and q advance at the speed of 1 and 2 respectively. Can I move forward at another speed?

Assume that p and q advance at the velocity V1 and V2 respectively. If a ring exists, when the pointers p and q enter the ring for the first time, their offset addresses relative to the first node in the ring are A and B respectively (the offset addresses can be understood as the number of nodes)

In this way, we can see that when a chain table has a ring, the sufficient condition is that, in a loop, the values of the pointers p and q are equal, that is, they are equal to the offset of the first node in the ring. We set the number of nodes in the ring to N, and the program loops m times.

The following equation can be established: (mod (n) is the remainder of N)

(A + M * V1) mod (n) = (B + M * V2) mod (N)

If the maximum integer of MOD (n) on the left of the equation is K1, and the maximum integer of MOD (n) on the right of the equation is K2

(A + M * V1)-K1 * n = (B + M * V2)-K2 * n

Sort out the above equations:

M = | (k2-k1) * n + A-B)/(v2-v1) |

If equation ① is true, the number of cycles m must be an integer. Obviously, if the v2-v1 is 1, the equation is true.

So p and q respectively at the speed of V1 and V2 and | v2-v1 | is 1, according to the above algorithm can find out whether there is a ring in the linked list. Of course | v2-v1 | when not 1, it may also be possible to obtain qualified M.

[CPP]
View plaincopyprint?
  1. Bool isexitsloop (slist * head)
  2. {
  3. Slist * slow = head, * fast = head;
  4. While (Fast & fast-> next)
  5. {
  6. Slow = slow-> next;
  7. Fast = fast-> next;
  8. If (slow = fast)
    Break;
  9. }
  10. Return! (Fast = NULL | fast-> next = NULL );
  11. }
bool IsExitsLoop(slist *head){    slist *slow = head, *fast = head;    while ( fast && fast->next )     {        slow = slow->next;        fast = fast->next->next;        if ( slow == fast ) break;    }    return !(fast == NULL || fast->next == NULL);}

Time Complexity Analysis: assume that the length of the tail (outside the ring) is len1 (number of nodes), the length of the inner ring is len2, and the total length of the linked list is N, then n = len1 + len2. When the P step is 1 and the Q step is 2, it takes len1 time for the p pointer to reach the ring entrance. After p arrives at the entrance, Q is not sure where it is, but it must be within the ring, at this time, p and q start to catch up. It takes len2 time for Q to catch up with P (both p and q point to the ring entrance) and 1 step to catch up with P (P points to the ring entrance, Q points to the previous node of the ring entrance ). In fact, after each step, the distance between Q and P is closer. Therefore, the distance between Q and P can catch up with P. Therefore, the total time complexity is O (n), and N is the total length of the linked list.


2. Start from the head of the linked list and the collision point, scan the link step by step until the collision occurs. The collision point is the entrance of the ring.

The proof is as follows:

The linked list is similar to the number 6.

Assume that the length of the tail (outside the ring) is a (number of nodes), and the length of the ring is B.

The total length (also the number of summary points) is a + B.

Starting from scratch, 0 base number.

The node accessed in step I is represented by S (I. I = 0, 1...

When I <A, S (I) = I;

When I ≥ A, S (I) = a + (I-A) % B.

Analyze the catch-up process.

The two pointers move forward separately, assuming that the collision goes through step X. S (x) = S (2x)

Cycle of the ring: 2X = Tb + X. X = TB.

In addition, the collision must be in the ring, and cannot be in the tail segment, with x> =.

The connection point starts from step a, that is, S ().

S (A) = S (Tb + a) = S (x + ).

Conclusion: Step A from collision point X is the connection point.

According to the assumption that Yi Zhi S (A-1) in the tail segment, S (a) on the ring, and S (x + a) on the ring. Therefore, a collision can occur.

However, it is also a step forward and a connection point, so a collision is inevitable.

In summary, the first collision point is the connection point, which is synchronized from the X point and the starting point.

[CPP]
View plaincopyprint?
  1. Slist * findloopport (slist * head)
  2. {
  3. Slist * slow = head, * fast = head;
  4. While (Fast & fast-> next)
  5. {
  6. Slow = slow-> next;
  7. Fast = fast-> next;
  8. If (slow = fast)
    Break;
  9. }
  10. If (fast = NULL | fast-> next = NULL)
  11. Return NULL;
  12. Slow = head;
  13. While (slow! = Fast)
  14. {
  15. Slow = slow-> next;
  16. Fast = fast-> next;
  17. }
  18. Return slow;
  19. }
slist* FindLoopPort(slist *head){    slist *slow = head, *fast = head;    while ( fast && fast->next )     {        slow = slow->next;        fast = fast->next->next;        if ( slow == fast ) break;    }    if (fast == NULL || fast->next == NULL)        return NULL;    slow = head;    while (slow != fast)    {         slow = slow->next;         fast = fast->next;    }    return slow;}

Time Complexity Analysis: assume that the length of the tail (outside the ring) is len1 (number of nodes), and the length of the inner ring is len2. The time complexity is "Whether the ring exists in time complexity" + O (len1)


3. Starting from the collision point, the two pointers p and q, Q move forward with one step, and Q move forward with two steps. The number of operations performed by the next collision is the length of the ring. This is easy to understand. For example, when two athletes A and B start to run from the starting point, the speed of a is twice that of B. When a is playing a game, B just runs two laps, both A and B are at the starting point. In this case, the length of a is equal to the length of the ring.

Assume that the length of the tail (outside the ring) is len1 (number of nodes), and the length of the ring is len2. the time complexity is "Whether the ring exists in time complexity" + O (len2 ).


4. Method 1: point the next pointer of the End Node of the linked list A to the header node of the linked list B, thus forming a new linked list. The problem is to find whether the new linked list has loops.

The time complexity is the time complexity of the loop, that is, O (length (A) + Length (B). Two additional pointers are used.

Method 2: If two linked lists overlap, all the nodes after the two linked lists start from the intersection. Therefore, if they intersect, the last node must be in total. Therefore, the method to determine the intersection of two linked lists is to traverse the first linked list and remember the last node. Then traverse the second linked list and compare it with the last node of the first linked list. If the two nodes are the same, they will be the same.

Time Complexity: O (length (A) + Length (B), but only one additional pointer is used to store the last node.


5. Point the next pointer of the End Node of linked list A to the header node of linked list B to construct a ring. The problem is transformed into an entrance question for this ring.
Time Complexity: calculate the time complexity of the ring entry


6. Determine whether two linked lists A and B have rings (note: the intersection of two linked lists indicates that the ring belongs to two linked lists)

If there is only one ring, A and B cannot be connected.

If both of them have loops, find the ring entrance of a and determine whether it is on the B linked list. If so, it indicates that a and B are intersecting.

Time Complexity: "time complexity of the ring entrance problem" + O (length (B ))


7. Calculate the length of La and lb for the two linked lists A and B respectively (the length of the ring and the sum of the length from the ring to the entry point are the length of the linked list). See question 3.

If La> LB, then linked list a pointer first LA-LB, linked list B pointer and then start to go, then the two pointer encounter location is the first node of the intersection.

If LB> La, then linked list B pointer first LB-LA, linked list a pointer and then start to go, then the two pointer encounter position is the first node of the intersection.

Time Complexity: O (max (La, LB ))

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.