⭐️
# 題目敘述
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
# 解題思路
# 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; | |
} | |
} |
單字
** **
!! !!
片語 & 搭配詞
!! !!