题解 | #链表中环的入口结点#

链表中环的入口结点

http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4

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

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

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        // 头结点和相遇点 距离 入口的步数相同
        ListNode meetNode = findMeetNode(pHead);
        if(meetNode == null){ return null;}
        ListNode start = pHead;
        while(start != meetNode){
            start = start.next;
            meetNode = meetNode.next;
        }
        return start;
    }

    public ListNode findMeetNode(ListNode pHead){
        ListNode fast = pHead,slow = pHead ;
        while(fast != null && fast.next != null){
            fast = fast.next.next ;
            slow = slow.next ;
            if(slow == fast){
                return slow ;
            }
        }
        return null;
    }
}
全部评论

相关推荐

牛客85811352...:1希音不知道算不算大厂 2完全符合,过得很舒服, 3确实只有杂活 领导找我续签到明年3、4月我要继续吗。主要是边实习边秋招这段时间还是有点累
什么是优秀的实习经历
点赞 评论 收藏
分享
SaviorSu:直接说下学期可以请假,一般情况学校允许我26届,大三就直接去实习了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务