Sort by

recency

|

1353 Discussions

|

  • + 0 comments

    I assumed you had to preserve the integrity of the data in the list, I did this as a joke lol

    def has_cycle(head):
        if head is None: 
            return 0
        
        while(True):
            if head.data == "foo": 
                return 1
            
            if head.next is None:
                return 0
            head.data ="foo"
            head = head.next
            
    

    Though replace foo with something cruder...

  • + 0 comments

    Isn't this part badly written?

    "head refers to the list of nodes 1 -> 2 -> 3 -> 1 -> NULL There is a cycle where node 3 points back to node 1, so return 1."

    It says there is a cycle but from 1 it points to NULL,shouldnt instead return to the first value ,as in photos?

  • + 1 comment

    Looks like someone messed up the running code?

  • + 0 comments

    My Java 8 Solution

    static boolean hasCycle(SinglyLinkedListNode head) {
            if (head == null) {
                return false;
            }
            
            SinglyLinkedListNode slow = head;
            SinglyLinkedListNode fast = head;
            
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
                
                if (slow.equals(fast)) {
                    return true;
                }
            }
            
            return false;
        }
    
  • + 1 comment

    Is something wrong with the tests? no solution is working here and this is a little awkward..