題目:
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