Linked List Cycle (Java)
来源:互联网 发布:百度云淘宝上怎么买 编辑:程序博客网 时间:2024/06/05 14:14
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
设两个指针,一个走两步,一个走一步,如果有环,快的一定会和慢的汇合
Source
/** * 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, q = head; while(q.next != null && q.next.next != null){ //由于此题不是找中间位置,所以可以写为判断本身和next的值是否为null,不影响 p = p.next; q = q.next.next; if(p == q) return true; } return false; }}
0 0
- Linked List Cycle Java
- Linked List Cycle (Java)
- (java) Linked List Cycle
- [Leetcode] Linked List Cycle (Java)
- Linked List Cycle II Java
- Linked List Cycle II (Java)
- [LeetCode][Java] Linked List Cycle
- 141. Linked List Cycle(java)
- 141. Linked List Cycle(Java)
- linked-list-cycle Java code
- JAVA-Linked List Cycle I&&Linked List Cycle II
- leetcode-Java-141. Linked List Cycle.java
- [Leetcode] Linked List Cycle II (Java)
- LeetCode141之Linked List Cycle java题解
- LeetCode 141 -Linked List Cycle ( JAVA )
- [LeetCode] 141. Linked List Cycle java
- [LeetCode] 142. Linked List Cycle II java
- LeetCode-141.142. Linked List Cycle (JAVA)链表找环
- 断电不断网——Linux的screen
- 【Cocos2d-x】实现资源热更新
- 黑马程序员——GUI 网络编程 正则
- 【 Visual C++】游戏开发笔记之二——最简单的DirectX,vc窗口的编写
- andriod bitmap使用
- Linked List Cycle (Java)
- 第四章思维导图
- 黑马程序员————结构体
- zoj1985 hdu1506 Largest Rectangle in a Histogram (动态规划)
- 本人的开发环境备注
- 《数据结构》实验三: 栈和队列实验
- 【Matlab学习】 遍历文件夹下的所有文件
- 设计模式-单例模式
- opencv6.1-imgproc图像处理模块之平滑与形态学操作