Sort by

recency

|

1364 Discussions

|

  • + 0 comments

    This works and returns as soon as it encounters cycle but not sure how effficient it is

        bool has_cycle(SinglyLinkedListNode* head) {
    
    if(head==NULL){
        return 0;
    }
        while (head->next!=NULL) {
                if (head->data==-300000) {
                return 1;
    
                }else{
                    head->data=-300000;
                }
                head=head->next;
        }
            return 0;
    

    }

  • + 0 comments

    Here is Cycle Detection solution in python, java, c++ and c programming - https://programmingoneonone.com/hackerrank-cycle-detection-problem-solution.html

  • + 0 comments

    Mi solución en Pyhton 3:

    def has_cycle(head):
        visited_nodes = []
    
        current = head
        while current:
            if current in visited_nodes:
                return 1
            visited_nodes.append(current)
            current = current.next
        return 0
    
  • + 0 comments

    I think I found a shortcut...

    bool has_cycle(SinglyLinkedListNode* head) {
        SinglyLinkedListNode*cur = head;
        int count = 0;
        while(cur->next != NULL){
            cur= cur->next;
            count++;
            if (count>1000)
            return 1;
        }
        return 0;
        
        
    }
    
  • + 0 comments
    bool has_cycle(SinglyLinkedListNode* head) {
       auto sentinel = 0xAABBCCDD;
       
       while (head)
       {
        if (head->data == sentinel)
            return 1;
            
        head->data = sentinel;
        head = head->next;
       }
       return 0;
    }