LeetCode——Linked List Cycle
来源:互联网 发布:ssm框架oa系统源码 编辑:程序博客网 时间:2024/05/22 06:46
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
原题链接:https://oj.leetcode.com/problems/linked-list-cycle/
题目:给定一个链表,判断它是否有环。
继续:
你能不用额外的空间解决吗?
思路:使用两个指针fast,slow,fast每次向前走两步,slow每次向前走一步,如果二者对应的节点相等,即存在环。
public boolean hasCycle(ListNode head) {ListNode fast = head,slow = head;if(head == null || head.next == null)return false;while(fast != null && fast.next != null){slow = slow.next;fast = fast.next.next;if(slow == fast)return true;}return false; }// Definition for singly-linked list.class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}}
0 0
- LeetCode——Linked List Cycle
- LeetCode——Linked List Cycle II
- LeetCode——Linked List Cycle
- Linked List Cycle —— Leetcode
- LeetCode——Linked List Cycle II
- leetcode 141 —— Linked List Cycle
- LeetCode题解——Linked List Cycle
- LeetCode——141. Linked List Cycle
- LeetCode——Linked List Cycle
- LeetCode — Linked List Cycle 解题报告
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- LeetCode——141. Linked List Cycle && 142. Linked List Cycle II
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- 多播(组播)原理分析
- HDOJ1018 Big Numbers
- library cache lock诊断思路
- 每一个优秀的人,都有一段沉默的时光
- Struts2快速入门
- LeetCode——Linked List Cycle
- Effective C++ Item 26 尽可能延后变量定义式的出现时间
- 微软Win8.1游戏开发Project Spark,10月见
- POJ 2253 Frogger
- hive (union & subquery)
- Tiny210 s5pv210 uboot移植(编译过程)
- SpringMVC+JDBC
- Spark部署及应用
- HDU1403 Longest Common Substring