Skip to content

Commit 48976fe

Browse files
committed
链表环检测
1 parent a227f32 commit 48976fe

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/main/java/code/collection/list/SingleLinkedList.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,15 @@ public boolean isCycle(){
213213
Node<E> p = head;
214214
Node<E> q = head;
215215
while (true) {
216-
p = p.next;
217216
if (q.next == null || q.next.next == null) {
218-
q = null;
219-
break;
220-
} else {
221-
q = q.next.next;
217+
return false;
222218
}
223-
if (p == q && p != null) {
219+
p = p.next;
220+
q = q.next.next;
221+
if (p == q) {
224222
return true;
225223
}
226224
}
227-
return false;
228225
}
229226

230227
@Override

0 commit comments

Comments
 (0)