题目:
题目描述 PHP 实现一个链表中包含环,请找出该链表的环的入口结点。
描述:
一个链表中包含环,请找出该链表的环的入口结点。
<?php
/*class ListNode{
var $val;
var $next = NULL;
function __construct($x){
$this->val = $x;
}
}*/
function EntryNodeOfLoop($pHead)
{
if($pHead == NULL || $pHead->next == NULL){
return NULL;
}
$p1 = $pHead;
$p2 = $pHead->next;
while($p2){
$tmp = $p2->next;
$p1->next = NULL;
$p1 = $p2;
$p2 = $tmp;
}
return $p1;
}