【LeetCode练习题】Linked List Cycle II
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?Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return
null.Follow up:
Can you solve it without using extra space?
题目意思:
第一个是判断链表里有没有环
第二个是如果有环,返回环的第一个节点,否则返回null。
因为两题解起来差不多,所以就做第二个就行了。
解题思路:
解法一:
注意,题目中有一个follow up。说接下来能不能在不使用额外空间的情况下解决。
好,我们就先来使用额外空间的。
首先想到的就是用hashmap。每遍历一个节点就存到hashmap里去,然后判断hashmap里是不是已经包含了下一个节点,如果包含,则说明是有环的,如果都遍历到最后一个节点了都不包含,就说明没有环存在。
好像C++的stl里头有一个hash_map容器,但是我更熟悉的是Java的HashMap,所以这一题果断选择用Java来做。
代码是酱紫的。
public class Solution {
public ListNode detectCycle(ListNode head) {
HashMap<ListNode,Integer> map = new HashMap<ListNode,Integer>();
while(head != null){
if(map.containsKey(head))
return head;
map.put(head,0);
head = head.next;
}
return null;
}
}
解法二:
好,看完了额外空间的,接下来就是不适用HashMap,在原来的链表上直接计算了。
使用两个指针……
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(true){
if(fast == null || fast.next == null){
return null;
}
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
break;
}
int lenCal = 0;
do{
slow = slow.next;
lenCal++;
}
while(slow != fast);
slow = head;
fast = head;
for(int i = 0; i < lenCal; i++){
fast = fast.next;
}
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(true){
if(fast == null || fast.next == null){
return null;
}
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
break;
}
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
以上三种方法都能AC。
【LeetCode练习题】Linked List Cycle II的更多相关文章
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- (链表 双指针) leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- leetcode 142. Linked List Cycle II ----- java
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
随机推荐
- android 中FragmentActivity中模拟返回键返回上一个Activity效果
FragmentTransaction中先加入一个Fragment,这个Fragment就是当前要显示的Fragment, 当通过事件触发显示第二个Fragment时,在加入第二个Fragment并调 ...
- Linux dirname、basename(转)
首先使用 --help 参数查看一下.basename命令参数很少,很容易掌握. $ basename --help 用法示例: $ basename /usr/bin/sort 输出&q ...
- bzoj1632 [Usaco2007 Feb]Lilypad Pond
Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼.这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方 ...
- 使用fdisk进行磁盘管理
http://itercast.com/lecture/17 disk是来自IBM的老牌分区软件,几乎所有Linux系统均默认安装 fdisk是一个MBR分区工具,不可用于GPT分区 只有超级用户(r ...
- Cocos2d-x游戏开发CCBAnimationManager控制动画
CocosBuilder能方便的编辑各种动画.大部分动画都是以独立片段的形式存在的. 须要由程序来控制何时播放. 管理ccbi文件的动画播放有个专门的类:CCBAnimationManager 大致的 ...
- 使用bulkCopy心得
最近一直在到excel导入,无意中发现Bulk Insert 批量导入,于是研究了一下,在测试的时候一直有问题,然后找度娘帮忙,说新增DataTable数据结构的时候,每个列要与数据库设计时字段对应, ...
- sqlplus 链接数据库
实验目的:在虚拟机中用sqlplus工具访问真实机的数据库: 实验环境: 真实机(windows系统,数据库服务名 orcl): SQL> select * from v$version; BA ...
- seajs路径问题及源码分析
seajs如此神奇,究竟是如何做到的呢,想知基原理,方可看其源码~~之前冲忙写下的,可能有点乱哦~~有什么不对的,欢迎拍砖! 如果进入seajs了管理范围,那么路径分为: 1. / 或 ...
- springmvc+mybatis如何分层
通常情况下,我们之间调用mapper,spring会为我们注入其实现,很方便,mybatis也提供了一个generator供我们生成bean.dao接口等.但是总有一种感觉叫不爽,感觉除了bean和m ...
- python文件处理及装饰器
1.文件处理: Python处理文件的流程比较简单,大致分为以下几个: 打开文件==>处理文件==>生成新文件==>写入文件 先说怎么打开一个文件: 打开一个文件可以有多种写法,下面 ...