-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
68 lines (54 loc) · 1.59 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
package LinkedList.No_234_Palindrome_Linked_List;
import LinkedList.ListNode;
/**
* FileName: Solution
* Author: EdisonLi的家用MacBook Pro
* Date: 2019-02-04 10:18
* Description: Palindrome Linked List
* <p>
* Given a singly linked list, determine if it is a palindrome.
* <p>
* Example 1:
* <p>
* Input: 1->2
* Output: false
* Example 2:
* <p>
* Input: 1->2->2->1
* Output: true
* Follow up:
* Could you do it in O(n) time and O(1) space?
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null){
return true;
}
ListNode middleNode = findMiddleNode(head);
ListNode reverseMiddleLinkedList = reverseLinkedList(middleNode);
while (head != null && reverseMiddleLinkedList != null) {
if (head.val != reverseMiddleLinkedList.val) {
return false;
}
head = head.next;
reverseMiddleLinkedList = reverseMiddleLinkedList.next;
}
return true;
}
//快慢指针 找中点 一二步指针
private ListNode findMiddleNode(ListNode head) {
ListNode fp = head;
while (fp.next != null && fp.next.next != null) {
fp = fp.next.next;
head = head.next;
}
fp = head.next;
head.next = null;
return fp;
}
//逆序链表
private ListNode reverseLinkedList(ListNode tarNode) {
LinkedList.No_206_ReverseLinkedList.Solution solution = new LinkedList.No_206_ReverseLinkedList.Solution();
return solution.reverseList(tarNode);
}
}