题解 | #判断链表中是否有环#
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
此题较为简单,贴出代码,讲下思路即可
运用快慢指针,判断是否有环,如果无环,指向null,有环两个指针会重叠
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
public class Solution { public boolean hasCycle(ListNode head){ if (head==null)return false; ListNode cur1=head; ListNode cur2=head; while (cur2!=null&&cur2.next!=null){ cur1=cur1.next; cur2=cur2.next.next; if (cur1==cur2){ return true; } } return false; } }
相关推荐