My recursion solution with O(1), O(n)


  • 1
    T

    public class Solution {

    private ListNode top = null;
    public bool IsPalindrome(ListNode head) {
        if(head == null || head.next == null)
            return true;
            
        top = head;
        
        return dfs(head);
    }
    
    private bool dfs(ListNode head){
        if(head.next == null)
        {
            var d = top;
            top = top.next;
            return head.val == d.val;
        }
        
        if(!dfs(head.next) || top.val != head.val)
            return false;
        
        top = top.next;
        
        return true;
    }
    

    }


Log in to reply
 

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