offer--(21) The Entry node--java description of the ring in the link list __java

Source: Internet
Author: User

Written in front:

The sword refers to the offer, interview, is a classic problem, today to summarize, in fact, from this topic, can be resolved, extended a number of issues, such as: whether there is a linked list, if there is a ring, to find the link in the ring of the entry node, the length of the ring is how much.

Topic Link: https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4
(1) Whether there is a link in the list

It is relatively simple to judge if there is a ring in the list, the idea is to set two pointers, a quick pointer, a slow pointer, a quick pointer 2 steps at a time, and the slow pointer 1 steps at a time, so that if there are rings in the list, then two pointers will certainly meet in the ring.

Code implementation:

Public ListNode Entrynodeofloop (ListNode phead) {
		if (Phead = null | | phead.next = NULL) {return
			null;
		}
		ListNode fast = Phead;
		ListNode slow = phead;
		while (Fast!= null && slow!= null) {
			//Quick Pointer two steps at a time, slow pointer one step
			fast = Fast.next.next;
			slow = Slow.next;
			if (Fast.val = = slow.val) {return
				fast;
			}
		}
		return null;
	}
(2) The length of the ring in the list

In question 1, find the location of the meeting point, so that the slow node does not move, fast node each step, the next time you meet again, see fast the number of steps to go, the length of the ring.

Code implementation:

	public int Entrynodeofloop (ListNode phead) {
		if (Phead = = NULL | | phead.next = NULL) {return
			0;
		}
		ListNode fast = Phead;
		ListNode slow = phead;
		while (Fast!= null && slow!= null) {
			//Quick Pointer two steps at a time, slow pointer one step
			fast = Fast.next.next;
			slow = Slow.next;
			if (Fast.val = = slow.val) {
				ListNode p = fast.next;
				ListNode q = slow;
				int step = 1;
				while (P.val!= q.val) {
					p = p.next;
					step++;
				}
				return step;
			}
		return 0;
	}
The value of the step, which is the length of the ring of the linked list.
(3) The entry node of the ring in the linked list

This question is the original question of the offer of the sword, the solution to this problem is: when we found in the first question, the speed and velocity of the pointer in the list after the meeting point, we reset two nodes, node p point to the head node, node Q point to just meet the node (fast or slow can). Node p and node Q take 1 steps at a time, and two nodes for the first meeting, then the entry node of the ring in the list.

Code implementation:

/* Public
 class ListNode {
    int val;
    ListNode next = null;

    ListNode (int val) {
        this.val = val;
    }
}
*/Public
class Solution {public

listnode entrynodeofloop (ListNode phead) {
		if (Phead = null | | phead.next = = null) {return
			null;
		}
		ListNode fast = Phead;
		ListNode slow = phead;
		while (Fast!= null && slow!= null) {
			//Quick Pointer two steps at a time, slow pointer one step
			fast = Fast.next.next;
			slow = Slow.next;
			if (Fast.val = = slow.val) {
				//meet, the quick pointer points to the head pointer, the slow pointer in the meeting position, each step
				listnode p = phead;
				ListNode q = slow;
				while (P.val!= q.val) {
					p=p.next;
					Q=q.next;
				}
				return p;
			}
		}
		return null;
	}
}

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.