141. Linked List Cycle
来源:互联网 发布:数据化人生txt 编辑:程序博客网 时间:2024/05/16 05:12
题目:https://leetcode.com/problems/linked-list-cycle/
代码:
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) { HashSet<ListNode> m = new HashSet<>(); if(head==null||head.next==null) return false; while(head!=null) { if(m.contains(head)) return true; m.add(head); head = head.next; } return false; }}11ms=================================/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) { if (head == null || head.next == null) return false; ListNode p = head; ListNode q = head; while(q != null && q.next != null) { p = p.next; q = q.next.next; if (p == q) return true; } return false; }}1ms
0 0
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- MIU 备忘录
- [PHP] MVC思想----错误处理类
- 202. Happy Number
- android.text.format.DateFormat 日期时间解析类
- 文章标题
- 141. Linked List Cycle
- 控制音乐播放器的退出再次播放相同音乐的功能实现
- 121. Best Time to Buy and Sell Stock
- 21. Merge Two Sorted Lists
- 给学PHP、工作中在用PHP的朋友们推荐几本书 浏览:3734次 出处信息 原文:http://www.xingmo.cn/html/20090401/81.html PHP与
- 24. Swap Nodes in Pairs
- Adapter适配器与具体应用
- SpringMVC的RESTful(一)
- 345. Reverse Vowels of a String