forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP13_2.java
More file actions
27 lines (23 loc) · 779 Bytes
/
P13_2.java
File metadata and controls
27 lines (23 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package ch08;
import datatype.ListNode;
import java.util.Deque;
import java.util.LinkedList;
public class P13_2 {
public boolean isPalindrome(ListNode head) {
Deque<Integer> deque = new LinkedList<>();
// 연결 리스트를 데크에 삽입
ListNode node = head;
while (node != null) {
deque.add(node.val);
node = node.next;
}
// 데크가 모두 비거나(짝수 개일 때) 1개 이하(홀수 개일 때)가 될 때까지 비교
while (!deque.isEmpty() && deque.size() > 1) {
// 데크의 양끝을 추출해 팰린드롬 여부 확인
if (deque.pollFirst() != deque.pollLast()) {
return false;
}
}
return true;
}
}