Share my c code


  • 0
    K

    bool isPalindrome(struct ListNode* head) {

    if (head == NULL)
        return true;
        
    struct ListNode *slower = head, *faster = head, *middle = NULL;
    
    while (faster != NULL && faster->next != NULL) {
        slower = slower->next;
        faster = faster->next->next;
    }
    
    while (slower != NULL) {
        struct ListNode *next_node;
        
        next_node = slower->next;
        slower->next = middle;
        middle = slower;
        slower = next_node;
    }
    
    for (struct ListNode *cur = head, *rev = middle;
         rev != NULL; cur = cur->next, rev = rev->next) {
        if (cur->val != rev->val)
            return false;
    }
    
    return true;
    

    }


Log in to reply
 

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