-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
75 lines (59 loc) · 1.58 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* 234. Palindrome Linked List
Easy
3037
349
Add to List
Share
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null)
return true;
ListNode slow = head;
ListNode fast = head.next;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
//the number of nodes of first part would be equal or more than the second part
ListNode newHead = slow.next;
slow.next = null;
newHead = reverse(newHead);
while(newHead != null){
if(newHead.val != head.val)
return false;
newHead = newHead.next;
head = head.next;
}
return true;
}
private ListNode reverse(ListNode head){
ListNode pre = null;
ListNode cur = head;
while(cur!=null){
ListNode nex = cur.next;
cur.next = pre;
pre = cur;
cur = nex;
}
return pre;
}
public static void main(String[] args){
int[] arr = {1,2,3,3,2,1,1};
ListNode head = NodeUtil.createList(arr, arr.length);
// NodeUtil.printList(head);
// head = new Solution().reverse(head);
// NodeUtil.printList(head);
System.out.println(new Solution().isPalindrome(head));
}
}