Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question2_7 #93

Open
damluar opened this issue Jan 15, 2015 · 0 comments
Open

Question2_7 #93

damluar opened this issue Jan 15, 2015 · 0 comments

Comments

@damluar
Copy link

damluar commented Jan 15, 2015

I might be wrong, but your method isPalindromeRecurse can be simplified like this:

public static Wrapper isPalindromeRecursive(Node<Integer> node, int length) {
    if (length == 0) {
        return new Wrapper(node, true);
    } else if (length == 1) {
        return new Wrapper(node.next, true);
    }

    Wrapper result = isPalindromeRecursive(n.next, length - 2);
    result.palindrome = result.palindrome && node.data == result.node.data;
    result.node = node.next;
    return result;
}

The case when length == 2 will be covered by length == 0 if we return current node instead of null.
The return logic is also easier without conditionals.
Correct me, please, if there are flaws in my solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant