public boolean isPalindrome(ListNode head) { Stack<Integer> stack = new Stack<>(); ListNode tail = head; while (Objects.nonNull(tail)) { stack.push(tail.val); tail = tail.next; } while (!stack.empty()) { if (head.val == stack.pop()) { head = head.next; } else { return false; } } return true; }
Runtime: 11 ms, faster than 16.91% of Java online submissions for Palindrome Linked List.
Memory Usage: 51.2 MB, less than 32.72% of Java online submissions for Palindrome