forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path234_isPalindromeList.java
48 lines (45 loc) · 1.32 KB
/
234_isPalindromeList.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
45
46
47
48
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) return true;
ListNode fast = head;
ListNode slow = head;
// Be careful, it is checkint fast.next and fast.next.next!!!!!!
while(fast.next!=null && fast.next.next!=null){
slow = slow.next;
fast = fast.next.next;
}
ListNode second = slow.next;
slow.next = null;
ListNode first = head;
second = reverse(second);
// use second because first may have one more node than second
while(second!=null){
if(first.val!=second.val) return false;
first = first.next;
second = second.next;
}
return true;
}
public ListNode reverse(ListNode head){
if(head == null || head.next == null) return head;
ListNode p = head;
ListNode q = head.next;
head.next = null;
while(p!=null && q!=null){
ListNode next = q.next;
q.next = p;
p = q;
if(next!=null) q = next;
else break;
}
return q;
}
}