-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathPartitionList_86.java
More file actions
32 lines (27 loc) · 962 Bytes
/
Copy pathPartitionList_86.java
File metadata and controls
32 lines (27 loc) · 962 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
28
29
30
31
32
package LeetcodeFirstMonth;
import java.util.HashMap;
public class PalindromeLinkedList_234 {
public static boolean isListPalindromic(ListNode head) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
int index = 0;
for (ListNode node = head; node != null; node = node.next) {
hashMap.put(index++, node.val);
}
int ptr1 = 0, ptr2 = index - 1;
while (ptr1 <= ptr2) {
if (hashMap.get(ptr1++) != hashMap.get(ptr2--)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
ListNode list = new ListNode(1);
list.next = new ListNode(1);
list.next.next = new ListNode(3);
list.next.next.next = new ListNode(3);
list.next.next.next.next = new ListNode(1);
list.next.next.next.next.next = new ListNode(1);
System.out.println(isListPalindromic(list));
}
}