-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode234.java
38 lines (37 loc) · 1.13 KB
/
Leetcode234.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
public class Leetcode234 {
/**
* The stack interpertation is simple, but we need to iterate the entire linkedlist twice
* For this solution, we create a helper fucntion to reverse
* Then we use fast-slow pointer to find the mid point of the linkedlist
* Then we reverse that part.
* Then we can find out the answer.
* @param head
* @return
*/
public boolean isPalindrome(ListNode head) {
ListNode slow = head, fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
if(fast != null)slow = slow.next;
ListNode cur = head;
fast = reverse(slow);
while(fast != null){
if(cur.val != fast.val)return false;
cur = cur.next;
fast = fast.next;
}
return true;
}
private ListNode reverse(ListNode head){
ListNode newHead = null;
while(head != null){
ListNode temp = head.next;
head.next = newHead;
newHead = head;
head = temp;
}
return newHead;
}
}