목록이 회문 여부를 확인하는 목록을 확인합니다.
예 1 :
입력 : 1 -> 2 출력 : false로
예 2 :
입력 : 1 -> 2 -> 2 -> 1 출력 : true로
문제 해결 아이디어 :
스택을 비교함으로써
코드 :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
Stack<ListNode> stack = new Stack<>();
ListNode cur = head;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
cur = head;
while (cur != null) {
if (cur.val != stack.pop().val) {
return false;
}
cur = cur.next;
}
return true;
}
}