Whether a single-linked table has a ring (Java version) or a single-linked java

Source: Internet
Author: User

Whether a single-linked table has a ring (Java version) or a single-linked java
Question: judge whether a single-chain table L with a leading node has a ring

Solution:
In my opinion, this question is really hard to solve, so some opportunistic people use a large number of N to control the number of access elements when accessing elements in a single-link table, they think that if there is a null element before accessing the nth element, there is no loop in a single-chain table. Otherwise, a single-chain table has a ring. Their explanation is that they have accessed so many elements that no element is empty. A single-chain table may have loops. In addition, generally, no more than N elements are in a single-chain table, therefore, a single-chain table has a ring. We can only use this method.

To determine whether a single-linked table has a ring, the two pointers are equal. In addition, the two pointers start from the starting node in different steps. You can set two pointers: slow, fast, and slow. Each time you take one step, fast starts from the node and then goes two steps, the two pointers keep following until fast is empty. The single-linked table has no loops, or slow is equal to fast, the single-linked table has loops. This process ensures that slow will walk through all nodes before it, and the step of fast walking more than slow will increase from 1 to 1 at a time, when they encounter each other, the step size is a positive integer multiple of the number of nodes in the ring.


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 {public static boolean hasCircle (LNode L) {if (L = null) return false; // if the single-chain table is empty, the single-chain table does not have a ring 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 ;}}

Expansion: Determine whether a single-chain table with a leading node has a ring and find the entry node of the ring.

Solution:

It is recommended that the entry node from the first node to the ring should take step a, that is, there is a total of a node including the header node outside the ring, and there are B nodes in the ring. Assuming that slow follows step s, slow met fast for the first time. They must meet in the ring, and slow did not go from the ring's entry point to the ring's entry point again when they met, assuming that the distance from the ring's entry node to the ring's d step distance met, set the encounter node to meet.

At this time, let the header nodes point to fast, so that slow and fast take a step back each time. When they meet for the first time again, the encounter node is the entrance node of the ring.

The proof is as follows:

When slow takes another s step, it will go to the meet node again, and then fast will arrive at the meet node for the first time after s step, because both of them go one step at a time, from the beginning of fast entering the ring, fast and slow have been encountering each other. The first node they met is the entry node of the ring.

Public class LinkListUtli {// 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 a single-chain table is empty, the single-chain 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. next; q = q. next;} return null ;}}

Expansion: Determine whether the single-chain table of the leading node has a ring and determine the length of the ring.

Solution:

Set a pointer, q, to point to the entry node of the ring, and move q back until q is equal to the entry node of the ring again. At this time, the total number of steps taken by q is the length of the ring.

Public class LinkListUtli {// 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 a single-chain table is empty, the single-chain 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;} // calculate the length of the single-chain table loop public static int circleLength (LNode L) {LNode p = searchEntranceNode (L ); // find the entry node of the ring if (p = null) return 0; // if no ring exists, return 0 LNode q = p. next; int length = 1; while (p! = Q) {length ++; q = q. next;} return length; // return ring length }}



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.