141. Linked List Cycle
Posted jessie2009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了141. Linked List Cycle相关的知识,希望对你有一定的参考价值。
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
//Time: O(n), Space: O(1) public boolean hasCycle(ListNode head) { if (head == null) { return false; } ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) { return true; } } return false; }
以上是关于141. Linked List Cycle的主要内容,如果未能解决你的问题,请参考以下文章
算法分析如何理解快慢指针?判断linked list中是否有环找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycl(代码