Skip to content

Commit

Permalink
Merge pull request #408 from zhangguangying/master
Browse files Browse the repository at this point in the history
获取前置节点修改
  • Loading branch information
wangzheng0822 committed Nov 17, 2019
2 parents e0e4e68 + 4d5f410 commit d5ef94e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions php/06_linkedlist/SingleLinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function delete(SingleLinkedListNode $node)

// 获取待删除节点的前置节点
$preNode = $this->getPreNode($node);
if (empty($preNode)) {
return false;
}

// 修改指针指向
$preNode->next = $node->next;
Expand Down Expand Up @@ -121,7 +124,7 @@ public function getNodeByIndex($index)
*
* @param SingleLinkedListNode $node
*
* @return SingleLinkedListNode|bool
* @return SingleLinkedListNode|bool|null
*/
public function getPreNode(SingleLinkedListNode $node)
{
Expand All @@ -133,7 +136,10 @@ public function getPreNode(SingleLinkedListNode $node)
$preNode = $this->head;
// 遍历找到前置节点 要用全等判断是否是同一个对象
// http://php.net/manual/zh/language.oop5.object-comparison.php
while ($curNode !== $node && $curNode != null) {
while ($curNode !== $node) {
if ($curNode == null) {
return null;
}
$preNode = $curNode;
$curNode = $curNode->next;
}
Expand Down

0 comments on commit d5ef94e

Please sign in to comment.