带环链表 linked list cycle
1
[抄题]:
给定一个链表,判断它是否有环。
[思维问题]:
反而不知道没有环怎么写了:快指针fast(奇数个元素)或fast.next(偶数个元素) == null
[一句话思路]:
快指针走2步,慢指针走1步。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[题目变变变]:
2
[抄题]:
返回环的入口
[思维问题]:
[一句话思路]:
背就行
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[总结]:
[复杂度]:Time complexity: O() Space complexity: O()
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[题目变变变]:
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/ public class Solution {
/*
* @param head: The first node of linked list.
* @return: True if it has a cycle, or false
*/
public boolean hasCycle(ListNode head) {
// write your code here
if (head == null) {
return false;
}
ListNode fast = head.next;
ListNode slow = head;
while(fast != slow) {
if (fast == null || fast.next == null) {
return false;
}
fast = fast.next.next;
slow = slow.next;
} while (head != slow.next) {
head = head.next;
slow = slow.next;
}
return head;
}
}
带环链表 linked list cycle的更多相关文章
- [LintCode] Linked List Cycle(带环链表)
描述 给定一个链表,判断它是否有环. 样例 给出 -21->10->4->5, tail connects to node index 1,返回 true. 这里解释下,题目的意思, ...
- lintcode:带环链表
带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...
- LeetCode 141. 环形链表(Linked List Cycle) 19
141. 环形链表 141. Linked List Cycle 题目描述 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 ...
- [PHP] 算法-请找出带环链表的环的入口结点的PHP实现
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后 ...
- Lintcode 102.带环链表
------------------------ 只要设置两个指针,称为快慢指针,当链表没有环的时候快指针会走到null,当链表有环的时候快指针早晚会追上慢指针的. AC代码: /** * Defin ...
- [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 ...
- LeetCode 141:环形链表 Linked List Cycle
给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a l ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
随机推荐
- Sqoop+mysql+Hive+ Ozzie数据仓库案例
mysql 数据库脚本为: /*==============================================================*/ /* DBMS name: MySQL ...
- Android ffmpeg rtmp(source code)
souce code: Android.mk 编译生成APK需要调用的so文件 LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODU ...
- 对于入门Demo的看法
对于一些以前没有接触过的技术,网上一搜,了解一些介绍信息,原理以及一些代码API的大致了解,接下来应该开始一个小Demo例子. 网上现在好多的教材,多数不知是怎么写的,基本大致一个样,对于初学者没什么 ...
- php表达式
表达式是PHP中一个重要的概念,可以把表达式理解为“任何有值的东西”.在本教程中涉及到表达式的语法,我们以“expr”来表示表达式. 下面就是一个表达式: $x > $y; 在上面的例子中,当$ ...
- async await promise 执行时序
先用一个例子来说明async await promise的执行顺序 console.log('start'); async function test(){ console.log('111'); a ...
- DOS中判断进程是否存在的方法
这里分享的主要是通过批处理中先判断进程是否存在,然后再做出操作的实现代码,需要的朋友可以参考下 检测进程是否存在,并做出预定动作. tasklist /nh>d:\tddown~1\1.tx ...
- PHP获取跳转后的URL,存到数据库,设置缓存时间
<?php error_reporting(0); header("Content-Type: text/html; charset=utf-8"); $fid=$_GET[ ...
- 46. linux下解压.tar.gz文件
tar -zxvf jdk-7u55-linux-i586.tar.gz 步骤二:解压jdk-7u55-linux-i586.tar.gz ,执行以下命令: #mkdir /usr/local/jav ...
- c++builder 6 远程调试
Delphi7环境 一.目标远程机器: 安装服务光盘里有,单独安装,启动,启动后目标机的托盘图标中会出现一个小“虫子”debug的图标 bordbg61.exe D:\Program Files (x ...
- kafka 修改partition,删除topic,查询offset
修改分区个数: ./kafka-topics./kafka/<id_of_kafka> --alter --partitions 10 --topic test_topic 上面命令将te ...