leetcode141
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution
{
public bool HasCycle(ListNode head)
{
//if (head == null)
//{
// return false;
//}
//else
//{
// var temp = head;
// while (head.next != null)
// {
// var cur = head.next;
// if (temp == cur)
// {
// return true;
// }
// else
// {
// head = head.next;
// }
// }
// return false;
//} if (head == null) return false;
ListNode walker = head;
ListNode runner = head;
while (runner.next != null && runner.next.next != null)
{
walker = walker.next;
runner = runner.next.next;
if (walker == runner) return true;
}
return false;
}
}
https://leetcode.com/problems/linked-list-cycle/#/description
补充一个python的版本:
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head == None:
return False
slow,fast = head,head
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
leetcode141的更多相关文章
- 每天一道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 ...
- [LeetCode141]Linked List Cycle
题目:Given a linked list, determine if it has a cycle in it. 判断一个链表是否有环 代码: /** * Definition for singl ...
- [Java]LeetCode141. 环形链表 | Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- LeetCode141.环形链表
给定一个链表,判断链表中是否有环. 进阶:你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * class ListNode { * i ...
- LeetCode141:Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- LeetCode141 环形链表(Java—HashSet简单应用or双指针)
题目: 判断给出的链表中是否存在环. 思路: 1. 遍历整个链表,将走过的节点的内存地址保存下来,如果再次走到同样的内存地址,说明链表中有环.时间复杂度为O(n). 2. 设置两个指针,fast指针每 ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
- LeetCode通关:听说链表是门槛,这就抬脚跨门而入
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master https://github.com/ch ...
随机推荐
- Hadoop & Spark
Hadoop & Spark 概述 Apache Hadoop 是一种通过服务集群并使用MapReduce编程数据模型完成大数据的分布式处理框架,核心模块包括:MapReduce,Hadoop ...
- 18-09-19 关于outlook的使用
- offsetWidth与clientWidth 区别
offsetWidth //元素宽度.内边距和边框,不包括外边距 offsetHeight //元素高度.内边距和边框,不包括外边距 clientWidth //元 ...
- linux c 获取console 结果
getLine(char *line, const char *cmd) { FILE *pf = popen(cmd, "r"); if (pf == NULL) { ; } f ...
- sklearn.preprocessing.LabelBinarizer
sklearn.preprocessing.LabelBinarizer
- day03运算符 逻辑运算符
今日内容 运算符 算术运算符 取模% 打印1~100基数 #模2余1的为基数 #以1 3 5 7 9结尾的为奇数 # count =1 # while count<100: # print(co ...
- C#语言与变量、数据类型
一.计算机语言 1.计算机语言包括:C\PHP\Ruby\JAVA\C#\Basi\JS\C++ 2.计算机语言发展史:机器语言——汇编语言——高级语言 二.代码 1.程序始终:Code(编码).Co ...
- Arch Linux pacman 与其他发行版操作比较
原文:https://wiki.archlinux.org/index.php/Pacman/Rosettahttps://old-en.opensuse.org/Software_Managemen ...
- IntelliJ IDEA创建maven多模块项目
创建完成后的目录结构如下: 一.项目主要分成3个模块,yves-parent为父模块,yves-dao和yves-web(webapp类型的模块)为子模块. 二 .Parent Project,创建y ...
- PythonStudy——列表类型 List type
# 1.定义 ls = [3, 1, 2] # 语法糖 | 笑笑语法 print(ls) ls = list([3, 1, 2]) # 本质 print(ls) # 嵌套 ls = [3, 1, [3 ...