Loop related issues of a single-chain table

Source: Internet
Author: User

Given a single-chain table, only the early pointer h is given:

1. How to determine whether a ring exists?

Proof:


When slow enters the loop at point A for the first time, fast must be somewhere at point B in the loop. At this time, the length of slow from head is x, the length of B from point A is y, and the cycle length is s. Because the step difference between fast and slow is 1, when the distance between slow and slow is y, it will be caught up by fast at M. Because y

// Determine whether a single-chain table has a public static boolean hasCycle (ListNode head) {if (head = null | head. next = null) {return false;} ListNode slow = head; ListNode fast = head; while (fast. next! = Null & fast. next. next! = Null) {fast = fast. next. next; slow = slow. next; if (fast = slow) {return true ;}} return false ;}

2. How do I know the ring length?

Proof: (there is a formula)


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> // return loop Length public static int extends elength (ListNode head) {if (head = null | head. next = null) {return 0;} ListNode slow = head; ListNode fast = head; while (fast. next! = Null & fast. next. next! = Null) {fast = fast. next. next; slow = slow. next; if (fast = slow) {fast = fast. next. next; int length = 2; while (fast! = Slow) {fast = fast. next. next; length = length + 2;} return length;} return 0 ;}

3. How do I find out the connection points of the ring?

Proof: When fast and slow met for the first time, assuming that slow took n steps, the entrance of the loop was completed at step x, then there are

Slow path: x + y = n; y indicates the distance between fast and slow M and the loop entry.

Fast path: x + y + k * s = 2 * n; s is the circumference of the loop, and k is an integer.

Therefore, n = k * s is obtained;

Obviously, if we start from x + y and slow takes n more steps, we can return to the point x + y (around k circles)

At the same time, if another pointer temp starts from the beginning, after n steps, it will also reach x + y.

Both of them subtract step y. We can see that the two will inevitably encounter at the entrance point of the loop after step x.

// Locate the starting point of the ring. public static ListNode getCycleStartNode (ListNode head) {if (head = null | head. next = null) {return null;} ListNode slow = head; ListNode fast = head; while (fast. next! = Null & fast. next. next! = Null) {fast = fast. next. next; slow = slow. next; if (fast = slow) {fast = head; while (fast! = Slow) {fast = fast. next; slow = slow. next;} return slow;} return null ;}

4. What is the length of the linked list with loops?

Proof: total length = s + x;

// The total length of the linked list with loops. 0 indicates no public static int getListLength (ListNode head) {if (head = null | head. next = null) {return 0;} ListNode slow = head; ListNode fast = head; while (fast. next! = Null & fast. next. next! = Null) {fast = fast. next. next; slow = slow. next; if (fast = slow) {ListNode p = slow; // mark the place where you met fast = head; int lengthX = 0; int lengthS = 0; while (fast! = Slow) {fast = fast. next; slow = slow. next; lengthX ++;} lengthS = lengthX; while (slow! = P) {slow = slow. next; lengthS ++;} return lengthX + lengthS ;}} return 0 ;}





Related Article

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.