141. Linked List Cycle(判断链表是否有环)
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?
利用快慢指针,如果相遇则证明有环
注意边界条件: 如果只有一个node.
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null) return false;
ListNode slower =head,faster = head;
while(faster!=null && faster.next!=null){
if(faster==slower) return true;
faster = faster.next.next;
slower = slower.next;
}
return false;
}
}
141. Linked List Cycle(判断链表是否有环)的更多相关文章
- LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [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 ext ...
- 141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
随机推荐
- mac 环境配置
安装homebrew 用于安装各种软件 eg:brew search qq 查看qq安装目录 brew install 复制刚刚查看到的目录安装qq 安装 oh my zsh 自动补全目录跳转 1.安 ...
- 远程执行命令:paramiko
paramiko模块用于通过 ssh 登录到远程客户端主机并执行命令,常见用法如下: [root@localhost ~]$ yum install -y python-paramiko 通过用户名密 ...
- MinGW和MSYS的自动安装 【转】
好吧,这是今天写的第二篇文章.我表示这篇才是今天的重头.(伪 半年之前曾经写过一篇关于MinGW和MSYS的手动安装的文章[1],到现在还是全站点击率最高的.(目前这篇文章是最高的.)好吧,侧面反映了 ...
- win7(64)使用vim碰到的奇怪问题
一直使用conemu做控制台使用vim,操作系统win7 64位,一直用的很好. 今天使用gvim打开文件发现c:\program file(x86)\vim\_vimrc不生效,最奇怪的是,采用控制 ...
- LeetCode——Happy Number
Description: Write an algorithm to determine if a number is "happy". A happy number is a n ...
- 使用CDN的网络访问过程
CDN是指内容分发网络,在网络各处架设节点服务器,当用户访问时,CDN系统会根据网络流量.到用户的距离等因素将请求导向离用户最近的节点上. 访问过程是: 1.用户向浏览器提供要访问的域名. 2.浏览器 ...
- js+jquery(二)
1.获取列表框所选中的全部选项的值 $("select").change(function() { // 设置列表框change 事件 // 获取列表框所选中的全部选项的值 ale ...
- 如何查找元素对应事件的js代码
以chrome的firebug为例 1.找到其dom元素,然后右键"break on"-->"subtree modification"等,设置后元素旁边 ...
- [算法] N 皇后
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- shell中的多进程【并发】(转)
http://bbs.51cto.com/thread-1104907-1-1.html