Just sharing my c++ code


  • 1
    bool isPalindrome(ListNode* head) {
        if (!head || !head -> next)
    	    return true;
    	ListNode *fast = head->next, *slow = head, *slownext=nullptr, *slowprev = nullptr;
    	while (fast) {
    		slownext = slow->next;
    		slow->next = slowprev;
    		if (fast->next && fast->next->next) {
    			fast = fast->next->next;
    			slowprev = slow;
    			slow = slownext;
    		}
    		else 
    		    break;
    	}
    	if (fast && fast->next) 
    	    slownext = slownext->next;
    	while (slow) {
    		if (slow->val != slownext->val)
    		    return false;
    		slownext = slownext->next;
    		slow = slow->next;
    	}
    	return true;
    }

Log in to reply
 

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