-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPalindromeLinkedList.java
More file actions
69 lines (51 loc) · 1.32 KB
/
PalindromeLinkedList.java
File metadata and controls
69 lines (51 loc) · 1.32 KB
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
package linked_list;
/**
* @Author: Wenhang Chen
* @Description:请判断一个链表是否为回文链表。 示例 1:
* <p>
* 输入: 1->2
* 输出: false
* 示例 2:
* <p>
* 输入: 1->2->2->1
* 输出: true
* @Date: Created in 10:24 1/11/2020
* @Modified by:
*/
public class PalindromeLinkedList {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}
ListNode slow = head, fast = head;
ListNode pre = head, prepre = null;
while (fast != null && fast.next != null) {
// fast到最后时,slow为中间
// 在遍历过程中翻转前半部分链表
pre = slow;
slow = slow.next;
fast = fast.next.next;
pre.next = prepre;
prepre = pre;
}
if (fast != null) {
// 如果为奇数个,则slow要往后一次
slow = slow.next;
}
while (pre != null && slow != null) {
if (pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
}