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 ...
随机推荐
- drf的组件和解析器
drf的序列化组件: 1. 用途: 把python中的对象,转成json格式字符串 2. 使用步骤1: 写一个类继承Serializer或者ModelSerializer 举例(类中选取字段进行序列化 ...
- 3.5 unittest生成测试报告HTMLTestRunner
3.5 unittest生成测试报告HTMLTestRunner 前言批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的.unittest里面是不 ...
- Post Order traverse binary tree using non-recursive way
Process analysis Stack = 5, Push 3, Stack = 5, 3. Pre = 5 Current = 3, Pre = 5, Push 2 to the st ...
- 【Java】字符串工具类
import android.annotation.SuppressLint; import java.io.UnsupportedEncodingException; import java.uti ...
- UVa699
这个建树的根选的很有意思,在中间作为树的根.所以二叉树建树的方法虽然一般是有两种数组的方法,一个是如果深度不太大的话,可以之间用2*k+1,2*k建树,如果很大的话,就挨着建树,弄一个结构体,有左右子 ...
- 磁盘操作系统 cmd命令
DOS CMD :磁盘操作系统 不区分大小写 **cd \ 根目录 cls 清空屏幕 dir 显示目录 d: 进入D盘 cd 进入目录命令 dir 查看当前目录的文件与目录 del 删除文件 del ...
- hibernate---级联保存、级联删除
直接上菜: dept.hbm.xml:关键点标蓝色这部分,inverse表示放弃维护外键关系,cascade就不用说了 <hibernate-mapping package="com. ...
- 中国商用分组密码SM4
SM4 密码算法结构 SM4 密码算法基本运算有两部分: 1.模2加:⊕,32比特异或运算 2.循环移位: <<< i ,把32位字循环左移i位 基本密码部件为 1非线性字节变换S盒 ...
- best practices for designing web api
restful why: meaningful This will be improve efficiency , less documents , just read the code auto g ...
- LG1337 [JSOI2004]平衡点 / 吊打XXX
题意 题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不 ...