Java easy-understanding answer, average runtime


  • 0
    Y
    public boolean isPalindrome(ListNode head) {
        if(head == null) return true;
        Stack<Integer> stack = new Stack<Integer>();
        ListNode node = head;
        while(node.next != null){
            stack.push(node.val);
            node = node.next;
        }
        stack.push(node.val);
        while(head.next != null){
            if(stack.pop() != head.val) return false;
            else head = head.next;
        }
        if(stack.pop() != head.val) return false;
        return true;
    }

Log in to reply
 

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