C Time O(n) Space O(1) but stupid way


  • 0
    T
    struct ListNode * reverseList(struct ListNode * head) {
    	struct ListNode * prev = NULL, * current = head;
    	while (current) {
    		struct ListNode * temp = current->next;
    		current->next = prev;
    		prev = current;
    		current = temp;
    	}
    	return prev;
    }
    
    bool hasCycle(struct ListNode *head) {
    	if (!head || head->next == NULL) {
    		return false;
    	}
    	struct ListNode * end = reverseList(head);
    	reverseList(end);
    	return end == head;
    }
    

  • 0
    F

    How do you reverse a list if there exists cycle?
    What does that mean?


Log in to reply
 

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