How can I determine whether a linked list is intersecting?

Source: Internet
Author: User

 

Question 1: Determine whether two linked lists are intersecting: (assume that both linked lists have no rings)
1. Determine whether each node of the first linked list is in the second linked list.
2. Connect the second linked list to the end of the first linked list to check whether the linked list has a ring and if it has a ring
3. traverse the first linked list first, remember the last node, and then traverse the second linked list to get a comparison between the last node and the last node of the first linked list.

 

 

 

Question 2: How to determine whether a single-chain table has a ring? (Do not use a flag. You can use up to two additional pointers)
The O (n) method is (using two pointers, one increment step at a time and one increment two steps at a time. If there is a ring, the two will inevitably overlap, and vice versa ):

Bool check (const node * head) <br/>{< br/> If (Head = NULL) return false; <br/> node * low = head, * fast = head-> next; <br/> while (fast! = NULL & fast-> next! = NULL) <br/>{< br/> low = low-> next; <br/> fast = fast-> next; <br/> If (Low = fast) return true; <br/>}< br/> return false; <br/>}< br/> 

 

Extension 1: If the linked list may have a ring, how can we determine whether two linked lists are intersecting?
Train of Thought: The step 1 of the linked list is 1, and the step 2 of the linked list is 2. If there is a ring and the intersection is met, it will certainly meet; otherwise, it will not intersection.

List1 head: P1 <br/> list2 head: P2 <br/> while (P1! = P2 & P1! = NULL & p2! = NULL) <br/>{< br/> P1 = p1-> next; <br/> If (P2-> next) <br/> P2 = P2-> next; <br/> else <br/> P2 = P2-> next; <br/>}< br/> If (p1 = P2 & P1 & p2) // intersection <br/> else // not intersection <br/> 

 

Extension 2: Find the first node of the intersection of two linked lists
Idea: In the process of determining whether the two linked lists are intersection or not, traverse the two linked lists and record their respective lengths. Let's write down the length of the two linked lists and traverse them again. The long chain table node starts to step forward (lengthmax-lengthmin), and then the two linked lists go forward simultaneously, each step, the first point of an encounter is the first point of the intersection of two linked lists.

Node * step (node * P, node * q) <br/>{< br/> If (! P |! Q) return NULL; <br/> int Plen = 1; <br/> int qlen = 1; <br/> bool result = false; <br/> while (p-> next) <br/> {<br/> Plen ++, P = p-> next; <br/>}< br/> while (Q-> next) <br/> {<br/> qlen ++, q = Q-> next; <br/>}< br/> result = (P = Q); <br/> If (result) <br/>{< br/> int steps = ABS (Plen-qlen); <br/> node * head = Plen> qlen? P: Q; <br/> while (Steps) // alignment processing <br/>{< br/> head = head-> next, steps --; <br/>}< br/> Plen> qlen? P = head: q = head; <br/> while (P! = Q) <br/>{< br/> P = p-> next, q = Q-> next; <br/>}< br/> reutrn P; <br/>}< br/> return NULL; <br/>}< br/> 

Extension 3: Determine whether a linked list has a ring.

Set two pointers (fast,
Slow), the initial values all point to the header, slow each forward step, fast each forward two steps, if the linked list has a ring, fast must first enter the ring, and slow then enter the ring, the two pointers must meet each other. (Of course, if the first line to the end of fast is null, It is a loop-free linked list) The program is as follows:

Bool isexitsloop (slist * head) <br/>{< br/> slist * slow = head, * fast = head; <br/> while (Fast & fast-> next) <br/>{< br/> slow = slow-> next; <br/> fast = fast-> next; <br/> If (slow = fast) break; <br/>}< br/> return! (Fast = NULL | fast-> next = NULL); <br/>} 

 

 

Expansion 4: Find the first node of the intersection of two linked lists
Idea: If fast meets slow, slow certainly does not traverse the linked list, and fast already loops n circles (1 <= N) in the ring ). Assume that slow takes s step, then fast takes 2 s step (the number of fast steps is equal to S
Plus N turns on the ring), set the ring length to R, then:

2 S = S + nR
S =
NR

Set the length of the entire linked list to L. The distance between the entrance ring and the encounter point is X, and the distance from the start point to the entrance point is.
A + x = nR
A + x = (n-1) R + R
(N-1) R + L-
A = (n-1) R + (L-a-x)

(L--
X) The distance from the encounter point to the ring entry point. From this point, we can see that from the chain table header to the ring entry point is equal to (n-1) the cycle inner ring + the encounter point to the ring entry point, so we set a pointer from the head of the linked list and from the encounter point. Each time we take a step, the two pointers must meet each other and the first point of the encounter is the ring entry point. The program is described as follows:

Slist * findloopport (slist * head) <br/>{< br/> slist * slow = head, * fast = head; <br/> while (Fast & fast-> next) <br/>{< br/> slow = slow-> next; <br/> fast = fast-> next; <br/> If (slow = fast) break; <br/>}< br/> If (fast = NULL | fast-> next = NULL) <br/> return NULL; <br/> slow = head; <br/> while (slow! = Fast) <br/>{< br/> slow = slow-> next; <br/> fast = fast-> next; <br/>}< br/> return slow; <br/>} 

 

 

 

 

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.