【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 ...
随机推荐
- 【转】 怎么刷入BOOT.IMG(刷机后开机卡在第一屏的童鞋请注意)-------不错不错
原文网址:http://bbs.gfan.com/android-3440837-1-1.html 之前呢,有好多机油问我关于刷机卡屏的问题,我解答了好多,但一一解答太费事了,在这里给大家发个贴吧.其 ...
- Word Break I II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- EasyUI的下拉选择框控件方法被屏蔽处理方式
1.html标签如下 <div id="selectMap" style="top: 1px;left: 80px;position: absolute;" ...
- $.cookie is not a function
在调试网站的时候,用jquery获取cookie时,报错: $.cookie is not a function; 一般情况$.cookie is not a function;错误原因: 一.没有引 ...
- Quartz 开源的作业调度框架
Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.本 ...
- 深入浅出:重温JAVA中接口与抽象的区别
抽象类:声明一个抽象类,就是在类的声明开头.在Class关键字的前面使用关键字abstract 下面定义一个抽象类,代码如下: abstract class A{ abstract void call ...
- Spring 注入数据源
一.在项目中添加dataSource所用到的包 dbcp数据源所需包: commons-dbcp.jar commons-pool.jar C3P0数据源所需包: c3p0-0 ...
- hbase 二级索引创建
在单机上运行hbase 二级索引: import java.io.IOException; import java.util.HashMap; import java.util.Map; import ...
- 运行加速多线程和GPU
http://blog.csdn.net/augusdi/article/details/11883003
- spring的IOC,DI及案例详解
一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...