Java Data structure interview problem-fast and slow pointer problem

Source: Internet
Author: User

The last time we learned the data structure of the ring list, let's take a look at the following questions,

determine if a one-way linked list is a ring-linked list?

Seeing this problem, someone is proposing to go through the linked list, remembering that the first element, when we iterate after the element reappears, is the circular linked list, if not this is a one-way non-circular linked list.

let us analyze the above solution, we analyze the time complexity of this program is O (n). So is it the best choice?

we introduce a new solution, that is, "fast and Slow pointer." Let's take a look at the next solution, whether it is better than the above ideas.

Ideas : When we traverse the linked list, we design two pointers, namely the fast pointer and the slow pointer, the block pointer walks 2 steps each time, and the slow pointer goes 1 steps each time, so that when we have two pointers in the list, if the second encounter will indicate that the list is a ring linked list.

Let's implement it in code.

/**

* See if there is a ring in the list

*

* @return

*/

Public boolean hasring () {

if (head! = null && size > 0) {

Node<t> fasttemp = head, lowtemp = head;

while (Lowtemp.getnext ()! = null && fasttemp.getnext ()! = null) {

Fasttemp = Fasttemp.getnext (). GetNext ();

Lowtemp = Lowtemp.getnext ();

if (lowtemp = = fasttemp) {

return true;

}

}

}

return false;

}

parsing: Defines two pointer nodes respectively is Node<t> fasttemp = head, lowtemp = head;

Each move starts from head. Then the next time the project, it is the fast hand walk two laps, slow hands walked a circle, the next encounter in situ position.

the next question is whether or not we need to be a quick and fast pointer to twice times faster when we are designing the speed pointer.

Assumptions: the speed pointer C Point met, then the meeting point between the loop point B is x so head node a away from B is k

At the time of the meeting

Slow pointer:   K + X + N (x+y) = m; ①

X+y The distance of a circle around a ring;n A slow pointer, a total of a few laps around the ring.

Quick pointer:

fast pointer is a slow pointer 2 times the speed , the same time they used when they met. Then the fast pointer traveled by the distance is 2*m;

K+x +n* (x+y) = 2*m; Ii

N is a fast pointer around the number of laps

②-① get the following formula

(n-n) * (x+y) = m;

Next, the ① will be put into the formula

(n-n) * (x+y) = k+x+n* (x+y);

here The X+y ring length is a fixed value. Assuming that the ring length is M

(n-n) *m = k+x+n*m; ----> deformation obtained k+x = (n-2*n) *m;

Formula : K+x = (n-2*n) *m

since we are designed to be O-cycle linked list, then the start is the loop point B, then (n-2*n) *m = 0.

N = 2*n; The number of rings around a fast-speed hand is twice times that of the latter, and the same time is used. It doesn't matter how many nodes there are in the ring.

Shanghai Still school Java Training Original, there are data organizations and so on Java technical articles, please pay more attention!

Let's look at an example:

given an unknown length of a unidirectional ring list, how to determine the node element in the middle of the linked list?

we can also use the "fast pointer" to achieve, when the quick pointer to walk around, and then stop, then the position of the slow pointer is the position of the middle element. At this time the complex O (n) = n

Java Data structure interview problem-fast and slow pointer problem

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.