-
Notifications
You must be signed in to change notification settings - Fork 8
/
PalindromeLinkedList.java
66 lines (53 loc) · 1.68 KB
/
PalindromeLinkedList.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package linkedlist;
// Source : https://leetcode.com/problems/linked-list-cycle-ii/
// Id : 142
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-05-29
// Topic : Linked list
// Other : this one should be a medium to my understanding.
// Tips :
// Result : 96.51% 99.89%
public class PalindromeLinkedList {
// this problem feels like it combines middle of the linked list/reverse linked list/ and part of cycle II together.
// 96.51% 99.89%
public boolean isPalindrome(ListNode head) {
if (head == null)
// that's what leetcode says, so
return true;
if (head.next == null)
return true;
ListNode curr = head, pre = null, next = null;
ListNode fast = head;
// reverse half of the list
while (fast != null) {
// we split fast's double next in order to handle odd/even number situation
fast = fast.next;
if (fast == null) {
// if the node has odd number, we need to move next ahead for once.
next = curr.next;
break;
}
fast = fast.next;
// do the reverse
next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
// count palindrome
while (next != null) {
if (pre == null || next.val != pre.val)
return false;
next = next.next;
pre = pre.next;
}
return pre == null ? true : false;
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}