Share a Java backtracking solution using backward scan without modifying original list


  • 1
    M
    public class Solution {
        boolean palindrome=true;
    	public boolean isPalindrome(ListNode head) {
    		readback(head,head);
    		return palindrome;
    	}
    	ListNode readback(ListNode back, ListNode forward){
    		if(back==null)
    			return forward;
    		ListNode next=readback(back.next, forward);
    		if(back.val!=next.val)
    			palindrome=false;
    		return next.next;
    	}
    }

Log in to reply
 

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