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的更多相关文章

  1. 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 ...

  2. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  3. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. [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 ...

  5. [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 ...

  6. 【题解】【链表】【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  7. (链表 双指针) 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 ...

  8. 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 ...

  9. 【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 ...

  10. 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 ...

随机推荐

  1. ubuntu下获取相应的内核源码

    一直以为是apt-get install ,apt-get search 也搜索不到相关的包,结果不是. 其实是 apt-get source linux-image-$(uname -r) 必须要用 ...

  2. 是否需要手动执行DataContext的Dispose方法?

    我们知道DataContext实现了IDisposable接口.在C#中,凡是实现了IDisposable接口的类,都推荐的使用using语句.如下: using (DataContext db = ...

  3. 【转】Android虚拟平台的编译和整合

    原文网址:http://blog.csdn.net/rickleaf/article/details/6369065 概要 Android从2008年开始到本文写的2011年,短短三年的时间里成为手机 ...

  4. Subsets 解答

    Question Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a su ...

  5. 剑指offer-面试题21.包含min函数的栈

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数. 在该栈中,调用min,push及pop的时间复杂度都是O(1). 这一题实际上需要一个辅助栈存储最小值: 1.在模板类定 ...

  6. Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏

    Number of Containers Time Limit: 1 Second Memory Limit: 32768 KB For two integers m and k, k is said ...

  7. POJ3071:Football(概率DP)

    Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2 ...

  8. es3中使用es6/7的字符串扩展

    最近在看阮一峰的<ES6标准入门>,在字符串扩展一节中有提到几个新的扩展,觉得挺有意思,想在ES3里面使用,于是就有下面的兼容性写法. repeat 将一个字符串重复n次 String.p ...

  9. hbase 二级索引创建

    在单机上运行hbase 二级索引: import java.io.IOException; import java.util.HashMap; import java.util.Map; import ...

  10. JavaScript事件属性绑定带参数的函数

    JavaScript中在对事件进行绑定的时候,往往是element.onclick=event;这种形式,这样使用的话则会出现无法传参数.因此我们可以使用function(){}匿名函数将事件包含其中 ...