⭐️

# 題目敘述

Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

palindrome
A palindrome is a sequence that reads the same forward and backward.

# Example 1

Input: head = [1,2,2,1]
Output: true

# Example 2

Input: head = [1,2]
Output: false

ListNode 的 class 內容

ListNode
// Definition for singly-linked list.
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

# 解題思路

# Solution

import java.util.Stack;
class Solution {
    public boolean isPalindrome(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        ListNode temp = head;
        while(temp != null){
            stack.push(temp.val);
            temp = temp.next;
        }
        while(head != null){
            if(head.val != stack.pop()){
                return false;
            }
            head = head.next;
        }
        return true;
    }
}


單字

** **
!! !!

片語 & 搭配詞

!! !!