Whether a single-chain table is intersection (Java version), single-chain java
Question: A single-chain table L1 and L2 with two leading nodes determines if they are intersecting (whether they have the same nodes)
Solution:
L1 and L2 are empty.
Because a single-chain table may have loops, we can discuss them in three cases.
1. There is no ring in L1 and L2, so you can judge whether the last node in them is the same and whether they are intersecting.
2. L1 and L2 have a ring and one does not have a ring, so they must not overlap.
3. L1 and L2 have loops, so they only Intersect when their rings are the same. When they have the same knots before they enter the ring, the number of their intersections is greater than the number of nodes in the ring. when they enter the ring, they have the first same knot point. The number of their intersections is equal to the number of nodes in the ring, in both cases, you can determine whether the entry nodes of the rings are the same to each other to determine whether they are intersecting.
ADT definition:
// The Node class LNode of the single-chain table {// to simplify the access to the single-chain table, the access permission for data items in the node is set to publicpublic int data; public LNode next ;}
Algorithm Implementation:
Public class LinkListUtli {// determines whether a single-chain table has a ring public static boolean hasCircle (LNode L) {if (L = null) return false; // when the single-chain table is empty, if (L. next = null) return false; // a single-link table has only the header node, and the next of the header node is empty. The single-link table does not contain the LNode p = L. next; // p indicates the pointer LNode q = L. next. next; // q indicates the pointer for taking two steps from the Start Node each time while (q! = Null) // if q is not null, execute the while LOOP {if (p = q) return true; // p is equal to q, and the single-chain table has a ring p = p. next; q = q. next. next;} return false;} // if a single-chain table does not have a ring, null is returned. If a ring exists, public static LNode searchEntranceNode (LNode L) is returned) {if (L = null) return null; // if the single-link table is empty, the single-link table does not contain the if (L. next = null) return null; // a single-link table has only the header node, and the next of the header node is empty. The single-link table does not contain the LNode p = L. next; // p indicates the pointer LNode q = L. next. next; // q indicates the pointer for taking two steps from the Start Node each time while (q! = Null) // if q is not null, execute the while LOOP {if (p = q) break; // p is equal to q, and the single-chain table has a ring p = p. next; q = q. next. next;} if (q = null) return null; // This is not the same as above. Let p and q take a step and then go to the loop to judge, this is because the header node may be the ring's entry node q = L; while (q! = Null) {if (p = q) return p; // return the entry node p = p in the ring. next; q = q. next;} return null;} // determines whether two single-chain tables overlap. public static boolean isIntersect (LNode L1, LNode L2) {if (L1 = null | L2 = null) return false; // L1. Any linked list in L2 is empty. They do not intersection. // L1. L2 has no if (! (HasCircle (L1) | hasCircle (L2) {LNode p = L1; LNode q = L2; while (p. next! = Null) {p = p. next;} while (q. next! = Null) {q = q. next;} if (p = q) return true; // two non-ring single-linked list tables. When the last node is the same, they return false;} // L1, both L2 else if (hasCircle (L1) & hasCircle (L2) {if (searchEntranceNode (L1) = searchEntranceNode (L2) return true; // two single-link tables with loops. The entry nodes of the rings are the same, and return false;} // L1. L2 contains loops, and else {return false; // two single-chain tables, one with a ring and the other without a ring, must not overlap }}}
Extended: A single-chain table L1 and L2 with two leading nodes to determine whether they are intersecting and find the first intersection node.
Solution:
When L1 and L2 intersect, if both L1 and L2 do not have loops, the absolute value of the difference between the number of nodes in two single-link tables is obtained. dCount, let the single-chain table with multiple nodes start from the beginning to go down the dCount step, let the single-chain table with fewer nodes start from the beginning to go back, and then let the two single-chain tables go one step at a time, until they are equal, this node is the first point of intersection. If both L1 and L2 have loops, calculate the total number of nodes from L1 and L2 to L2 in a single-chain table, and find the absolute value of the difference between the two, let the count-large single-chain table start from the beginning to go down the dCount step, let the count-large single-chain table start from the beginning to go back, and then let the two single-chain tables go back each time, until they are equal, this node is the first point of intersection.
Algorithm Implementation:
Public class LinkListUtli {// determines whether a single-chain table has a ring public static boolean hasCircle (LNode L) {if (L = null) return false; // when the single-chain table is empty, if (L. next = null) return false; // a single-link table has only the header node, and the next of the header node is empty. The single-link table does not contain the LNode p = L. next; // p indicates the pointer LNode q = L. next. next; // q indicates the pointer for taking two steps from the Start Node each time while (q! = Null) // if q is not null, execute the while LOOP {if (p = q) return true; // p is equal to q, and the single-chain table has a ring p = p. next; q = q. next. next;} return false;} // if a single-chain table does not have a ring, null is returned. If a ring exists, public static LNode searchEntranceNode (LNode L) is returned) {if (L = null) return null; // if the single-link table is empty, the single-link table does not contain the if (L. next = null) return null; // a single-link table has only the header node, and the next of the header node is empty. The single-link table does not contain the LNode p = L. next; // p indicates the pointer LNode q = L. next. next; // q indicates the pointer for taking two steps from the Start Node each time while (q! = Null) // if q is not null, execute the while LOOP {if (p = q) break; // p is equal to q, and the single-chain table has a ring p = p. next; q = q. next. next;} if (q = null) return null; // This is not the same as above. Let p and q take a step and then go to the loop to judge, this is because the header node may be the ring's entry node q = L; while (q! = Null) {if (p = q) return p; // return the entry node p = p in the ring. next; q = q. next;} return null;} // determines whether two single-chain tables overlap. public static boolean isIntersect (LNode L1, LNode L2) {if (L1 = null | L2 = null) return false; // L1. Any linked list in L2 is empty. They do not intersection. // L1. L2 has no if (! (HasCircle (L1) | hasCircle (L2) {LNode p = L1; LNode q = L2; while (p. next! = Null) {p = p. next;} while (q. next! = Null) {q = q. next;} if (p = q) return true; // two non-ring single-linked list tables. When the last node is the same, they return false;} // L1, both L2 else if (hasCircle (L1) & hasCircle (L2) {if (searchEntranceNode (L1) = searchEntranceNode (L2) return true; // two single-link tables with loops. The entry nodes of the rings are the same, and return false;} // L1. L2 contains loops, and else {return false; // two single-chain tables, one with a ring and the other without a ring, must not be intersecting} // calculate the public static LNode searchFirstIntersectNode (LNode L1, LNode L2) of the first intersection of the two linked lists) {if (isInter Sect (L1, L2) return null; // when L1 and L2 of a single-chain table do not intersect, nullif (! HasCircle (L1) // when there is no ring in L1 and L2, there is no ring {int count1 = 1; // number of nodes in L1 of a single-chain table, including the header node int count2 = 1; // number of nodes in the L2 of the single-chain table, including the header node LNode p = L1; LNode q = L2; while (p. next! = Null) {p = p. next; count1 ++;} while (q. next! = Null) {q = q. next; count2 ++;} if (count1> = count2) // a single-chain table L1 is longer than a single-chain table L2 or the same length as the two {int dCount = count1-count2; // The number of nodes in a single-chain table L1 that are more than that in a single-chain table L2 p = L1; q = L2; while (dCount> 0) {p = p. next;} while (p! = Null) {if (p = q) return p; p = p. next; q = q. next ;}} else {int dCount = count2-count1; // The number of nodes p = L1; q = L2 for a single-chain table L2 than that of a single-chain table L1; while (dCount> 0) {q = q. next;} while (q! = Null) {if (p = q) return p; p = p. next; q = q. next ;}} when else // L1 has a ring, both L1 and L2 have a ring {int count1 = 1; // The number of all the nodes between the first node and the entry node of the ring in the single-chain table L1, including the header node and the entry node of the ring int count2 = 1; // The number of all nodes in a single-chain table L2 from the first node to the entry node of the ring, including the header node and the entry node of the ring LNode p = L1; LNode q = L2; while (p! = SearchEntranceNode (L1) {p = p. next; count1 ++;} while (q! = SearchEntranceNode (L2) {q = q. next; count2 ++;} if (count1> = count2) // a single-chain table L1 is longer than the number of all nodes from the first node to the entry node of the ring in a single-chain table L2 or the same length between the two {int dCount = count1-count2; // The number of nodes in a single-chain table L1 is higher than that in a single-chain table L2 from the Start Node to the entry node of the ring. p = L1; q = L2; while (dCount> 0) {p = p. next;} while (p! = Null) {if (p = q) return p; p = p. next; q = q. next ;}} else {int dCount = count2-count1; // The number of nodes in a single-chain table L2 is higher than that in a single-chain table L1 from the Start Node to the entry node of the ring. p = L1; q = L2; while (dCount> 0) {q = q. next;} while (q! = Null) {if (p = q) return p; p = p. next; q = q. next ;}} return null ;}}