leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:
判断一个链表是否有环。
解决方案:
双指针,快指针每次走两步,慢指针每次走一步,
如果有环,快指针和慢指针会在环内相遇,fast == slow,这时候返回true。
如果没有环,返回false.
/*** 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) {ListNode fast = head, slow = head;if(head == null || head.next == null) return false;while(fast != null && fast.next != null){fast = fast.next.next;slow = slow.next;if(fast == slow){return true;}}return false;} }
转载于:https://www.cnblogs.com/iwangzheng/p/5695174.html
leetcode 141. Linked List Cycle相关推荐
- LeetCode 141. Linked List Cycle (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LeetCode] 141. Linked List Cycle 单链表判圆算法
TWO POINTER 快指针速度2 , 慢指针速度1 相对速度1,有环必然相遇 public class Solution {public boolean hasCycle(ListNode hea ...
- LeetCode 141 Linked List Cycle
用快慢指针来判定是否有环. 这里while loop里的条件,用的是fast.next != null && fast.next.next != null,保证如果没有环,slow一定 ...
- Leetcode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 141. Linked List Cycle
欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法
题目地址:Linked List Cycle - LeetCode Given a linked list, determine if it has a cycle in it. To represe ...
最新文章
- link 和@import 的区别?
- 一般物流网站建设有哪些必备版块?
- Linux之解析鼠标input事件数据
- 浅析HTML、CSS、JavaScript之间的联系与区别
- CentOS6.7安装SBT
- expect脚本的简单应用
- java 抽象类语法_JAVA基础语法8--多态/抽象类/抽象方法
- 【零基础学Java】—继承的概述(十九)
- LeetCode 724. Find Pivot Index
- Eclipse 工程配置与目录结构及各种文件夹(常用插件)
- [翻译]WPF控件库 MaterialDesignInXamlToolkit (2) Brush Names
- java 爬虫 html页面 parse,Java 爬虫 爬取html网页解析
- 写出线程同步相关的方法,以银行账号存储款为例
- 网易博客 android,android编译环境
- 可能是最简单暴力的卸载工具Geek Uninstaller
- 【Ps问题】PS旋转功能会让图片乱飞的解决方法
- 李嘉诚80个人生经典语录
- Linux好用命令之base64命令
- 沟通的艺术与处世智慧 ——戴尔卡耐基(笔记)
- yarn install出现异常 error An unexpected error occurred: “EIO: i/o error