-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (39 loc) · 934 Bytes
/
Solution.java
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
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
int length = 0,
index = 0;
ListNode p = head;
while (p != null) {
length++;
p = p.next;
}
p = null;
while (index < length / 2) {
index++;
ListNode temp = p;
p = head;
head = head.next;
p.next = temp;
}
if (length % 2 == 1) {
head = head.next;
}
while (p != null && head != null) {
if (p.val != head.val) {
return false;
} else {
p = p.next;
head = head.next;
}
}
return true;
}
}