141 Linked List Cycle 环形链表
给定一个链表,判断链表中否有环。
补充:
你是否可以不用额外空间解决此题?
详见:https://leetcode.com/problems/linked-list-cycle/description/
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){
return false;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
return true;
}
}
return false;
}
}
141 Linked List Cycle 环形链表的更多相关文章
- 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 ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- [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 ...
- 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判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 【LeetCode】Linked List Cycle(环形链表)
这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...
- 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 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
随机推荐
- 在C语言中使用libiconv进行编码转换的示例
libiconv_sample.c #include <stdio.h> #include <malloc.h> #include "libiconv/iconv.h ...
- 求a + aa + aaa + aaaa + aaaaa ...的值,例如:1 + 11 + 111,2 + 22 + 222 + 2222 + 22222
#include <stdio.h> unsigned superposition(unsigned m, unsigned n); int main() { printf("1 ...
- java 监听文件或者文件夹变化的几种方式
1.log4j的实现的文件内容变化监听 package com.jp.filemonitor; import org.apache.log4j.helpers.FileWatchdog; public ...
- Codeforces Round #352 (Div. 2) C. Recycling Bottles
C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- chdir函数的使用【学习笔记】
#include "apue.h" #include <fcntl.h> int main(void) { ) err_sys("chdir failed&q ...
- SDUT OJ 2054 双向链表的实现 (结构体node指针+遍历 *【模板】)
双向链表 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内 ...
- silverlight 使用IValueConverter 转换
在绑定数据中 有时候我们需要转换相关数据类型 silverlight提供了一个System.Windows.Data.IValueConverter接口它提供两个方法 Convert和ConvertB ...
- Python学习笔记_Redis
一.Redis介绍 非关系型数据库:mongodb.redis redis数据全部是存在内存里面的. redis本身每秒支持30w次的读写,本身性能非常好, redis重启后,数据会消失,持久性不好, ...
- SPOJ:House Fence(分治&DP)
"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...