Leetcode_num7_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?

这是一道关于链表比较简单的题,很顺利就解决了,不多说啦,上代码啦

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @return a boolean
    def hasCycle(self, head):
        if head==None:
            check=False
        else:
            check=True
            node=head
            while(node.val!=0):
                node.val=0
                node=node.next
                if node==None:
                    check=False
                    break
        return check
时间: 2024-10-05 04:55:14

Leetcode_num7_Linked List Cycle的相关文章

[leedcode 142] 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? 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!).  我们已经得到了结论a=c,那么让两个指针分别从X

jQuery图片切换插件jquery.cycle.js

Cycle是一个很棒的jQuery图片切换插件,提供了很好的功能来帮助大家更简单的使用插件的幻灯功能 下载cycle插件并引入,此时,注意把引入它的代码放在引入jQuery主文件之后. <head> <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript" src

Gartner&#39;s Hype Cycle for Emerging Technologies

2012 2013 2014 2015 2016 Gartner's Hype Cycle for Emerging Technologies

[LeetCode]Linked List Cycle

题目:Linked List Cycle 判断一个单链表中是否有环,要求常量空间复杂度: 思路: 使用两个指针同时从链表表头开始移动,一个移动一步,一个移动两步,直到两个指针重合或某一指针指向链尾. 两个指针重合则单链表有环存在,否则没有. 第二个指针以第一个指针的两倍的速度移动,而第一个指针每次移动一步,这样只要有环,两个指针必定能重合. bool LeetCode::hasCycle(ListNode *head){ if (!head)return false; ListNode *p =

Cycle Sort

Cycle sort的思想与计数排序太像了,理解了基数排序再看这个会有很大的帮助, 圈排序与计数排序的区别在于圈排序只给那些需要计数的数字计数,先看完文章吧,看完再回来理解这一句话 所谓的圈的定义,我只能想到用例子来说明,实在不好描述 待排数组[ 6 2 4 1 5 9 ] 排完序后[ 1 2 4 5 6 9 ] 数组索引[ 0 1 2 3 4 5 ] 第一部分 第一步,我们现在来观察待排数组和排完后的结果,以及待排数组的索引,可以发现 排完序后的6应该出现在索引4的位置上,而它现在却在位置0上

L - Points on Cycle

Description There is a cycle with its center on the origin. Now give you a point on the cycle, you are to find out the other two points on it, to maximize the sum of the distance between each other you may assume that the radius of the cycle will not

[Cycle.js] Hello World in Cycle.js

Now you should have a good idea what Cycle.run does, and what the DOM Driver is. In this lesson, we will not build a toy version of Cycle.js anymore. Instead, we will learn how to use Cycle.js to solve problems. We will start by making a simple Hello

[Cycle.js] Making our toy DOM Driver more flexible

Our previous toy DOM Driver is still primitive. We are only able to sends strings as the textContent of the container element. We cannot yet create headers and inputs and all sorts of fancy DOM elements. In this lesson we will see how to send objects

LeetCode(141): Linked List Cycle

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? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h