-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path234_isPalindrome.java
More file actions
40 lines (40 loc) · 1018 Bytes
/
234_isPalindrome.java
File metadata and controls
40 lines (40 loc) · 1018 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
33
34
35
36
37
38
39
40
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null)
return true;
ListNode h = new ListNode(-1);
h.next = head;
ListNode slow = h,quick = h;
while(quick!=null&&quick.next!=null){
slow = slow.next;
quick = quick.next;
if(quick != null)
quick = quick.next;
}
quick = slow.next;
slow.next = null;
while(quick!=null){
ListNode tmp = quick.next;
quick.next = slow.next;
slow.next = quick;
quick = tmp;
}
quick = slow.next;
slow = h.next;
while(quick!=null){
if(quick.val !=slow.val)
return false;
quick = quick.next;
slow = slow.next;
}
return true;
}
}