LeetCode :141. 环形链表

141. 环形链表

思路:思维别僵化嘛,给你们看看那些脑路清奇的写法。
1.测试处来最长的数据长度,然后就直接来减超过就是循环。
class Solution {
public:
    bool hasCycle(ListNode *head) {
        int cout=8029;
        while(head!=NULL && cout>0){
         head=head->next;
         cout--;
        }
         if(head == NULL) return false;
        return true;
    }
};
2.这个就是破环数据来达到目的。
class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        while head:
            if head.val == 'bjfuvth':
                return True
            else:
                head.val = 'bjfuvth'
            head = head.next
        return False

你可能感兴趣的:(力扣刷题)