Share on facebook , linkedln, twitter my simple solution for linked list


  • 0
    L

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

    from collections import defaultdict
    
        class ListNode(object):
            def __init__(self, x):
                self.val = x
                self.next = None
        # I used the hash function to uniquely identify the objects
        class Solution(object):
            def detectCycle(self, head):
                """
                Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
                :type head: ListNode
                :rtype: ListNode
                """
                if not head:
                    return None
        
                store_object = defaultdict(int)
        
                while head.next != None:
                    hash_value = hash(head)
                    if hash_value in store_object:
                        return head
                    store_object[hash_value] += 1
                    head = head.next
        
                return None

  • 0
    This post is deleted!

  • 1

    Given how complicated you made it, I wouldn't share it so much. Here's a much simpler version:

    def detectCycle(self, head):
        seen = {None}
        while head not in seen:
            seen.add(head)
            head = head.next
        return head
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.