The entry node of the link in the linked list-the point of the sword

Source: Internet
Author: User

Description of the entry node of the link in the linked list

A linked list contains the ring, please find the link to the list of the entry node.

Ideas
    1. If the list exists ring, set two pointers pslow and Pfast,pslow each step, pfast each walk two steps, when pfast catch up Pslow, pfast more than Pslow walk is just pslow walk is the number of nodes contained in the ring.
    2. So, the second walk, a start from the beginning, the other from the meeting node, will eventually meet in the ring entrance node
Code
/* public class ListNode {int val;    ListNode next = null;    ListNode (int val) {this.val = val; }}*/PublicClass Solution {Public ListNode Entrynodeofloop (ListNode phead) {if (Phead = =NULL) {ReturnNull }listnode pslow = Phead; ListNode pfast = Phead;do {if (Pfast. Next = = null) {return null; //has a null value indicating that the linked list has no ring}pslow = Pslow. next;pfast = pfast. next; if (Pfast. Next = = null) {return null; //has a null value indicating that the linked list has no ring}pfast = Pfast. next;} while (pslow! = pfast);p slow = phead; while (pslow! = pfast) {Pslow = Pslow. Next;pfast = Pfast. next;} return Pslow;}            

The entry node of the ring in the linked list-the offer of the sword *

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.