C++ solution using recursion


  • 0
    J
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
    		bool bRes = true;
    		ListNode* prev = head;
            check(head, prev, bRes);
    		return bRes;
        }
    	void check(ListNode* node, ListNode*& prev, bool& bRes)
    	{
    		if(!node) return;
    		check(node->next, prev, bRes);
    		if(prev->val != node->val)
    			bRes = false;
    		prev = prev->next;
    	}
    };

  • 0

    Mr gentlemen ,could you explain your codes?


  • 0

    Good solution. but recursion is actually using O(n) memory.


Log in to reply
 

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