Leetcode Linked List Cycle 循环链表
来源:互联网 发布:mac geforce now 教程 编辑:程序博客网 时间:2024/04/30 02:46
题目:
Given a linked list, determine if it has a cycle in it.
分析:
1. 使用快慢指针,如果有环,快慢指针会相遇,如果没有环,快指针会首先到达null。
2. fast = fast.next.next语句注意fast.next可能已经是null,这是会出现空指针错误。因此,需要先进行判断。
Java代码实现:
/** * 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 fast = head.next.next; ListNode slow = head.next; while(fast!=null ) { if(fast == slow) return true; if(fast.next!=null) fast = fast.next.next; else fast = fast.next; slow = slow.next; } return false; }}
0 0
- Leetcode Linked List Cycle 循环链表
- Leetcode Linked List Cycle II 循环链表入口
- LeetCode 141. Linked List Cycle(链表循环)
- LeetCode 142. Linked List Cycle II(循环链表)
- (LeetCode)Linked List Cycle --- 判断循环链表
- LeetCode 141 Linked List Cycle(循环链表)(HashSet/Linked List)
- leetcode---linked-list-cycle---链表中点
- [leetcode 141] Linked List Cycle----判断链表是否为循环链表
- leetcode 141. Linked List Cycle 链表循环的判定 + 双指针
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- [LeetCode]Linked List Cycle
- LeetCode | Linked List Cycle
- Leetcode: Linked List Cycle
- 一个简单的生产者消费者
- 黑马程序员----[20150507][集合框架4]
- APT攻击有何变化?
- 常用自定义C#类型转换函数
- 线性表链式存储
- Leetcode Linked List Cycle 循环链表
- Linux Shell 脚本攻略 学习笔记-1
- maven环境快速搭建
- maven 配置文件打jar 包
- 看过的有关js的经典(五)
- Loadrunner 11 测试API java Vuser 模拟Post,Get 请求实例
- scala的split()函数
- Bluemix SQL Database 如何使用
- linux服务器性能查看