File tree 1 file changed +8
-2
lines changed
1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,9 @@ public function delete(SingleLinkedListNode $node)
86
86
87
87
// 获取待删除节点的前置节点
88
88
$ preNode = $ this ->getPreNode ($ node );
89
+ if (empty ($ preNode )) {
90
+ return false ;
91
+ }
89
92
90
93
// 修改指针指向
91
94
$ preNode ->next = $ node ->next ;
@@ -121,7 +124,7 @@ public function getNodeByIndex($index)
121
124
*
122
125
* @param SingleLinkedListNode $node
123
126
*
124
- * @return SingleLinkedListNode|bool
127
+ * @return SingleLinkedListNode|bool|null
125
128
*/
126
129
public function getPreNode (SingleLinkedListNode $ node )
127
130
{
@@ -133,7 +136,10 @@ public function getPreNode(SingleLinkedListNode $node)
133
136
$ preNode = $ this ->head ;
134
137
// 遍历找到前置节点 要用全等判断是否是同一个对象
135
138
// http://php.net/manual/zh/language.oop5.object-comparison.php
136
- while ($ curNode !== $ node && $ curNode != null ) {
139
+ while ($ curNode !== $ node ) {
140
+ if ($ curNode == null ) {
141
+ return null ;
142
+ }
137
143
$ preNode = $ curNode ;
138
144
$ curNode = $ curNode ->next ;
139
145
}
You can’t perform that action at this time.
0 commit comments