-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeLinkedList.java
More file actions
executable file
·67 lines (60 loc) · 2 KB
/
Copy pathPalindromeLinkedList.java
File metadata and controls
executable file
·67 lines (60 loc) · 2 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
56
57
58
59
60
61
62
63
64
65
66
67
package list;
/**
* @Author: Mr.Z
* @DateTime: 2021/02/22 16:37
* @Description: 234. 回文链表 https://leetcode.com/problems/palindrome-linked-list/
* <p>
* 输入: 1->2->2->1
* 输出: true
*/
public class PalindromeLinkedList {
public boolean isPalindrome1(ListNode head) {
// 快慢指针
// 链表分为两部分=>翻转后半部分=>比较两部分是否相等=>恢复链表=>返回结果
// 1 ->2->2->1->null => 1->2->2->1->null => 1->2 null<-2<-1 (奇数节点)
// s/f s f h s
// 1 ->2->3->2->1->null => 1->2->3->2->1->null => 1->2 3<-2<-1 (偶数节点)
// s/f s f h s
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
if (fast != null) slow = slow.next;// 偶数节点,让 slow 指向下一个节点
slow = reverse(slow);
fast = head;
while (slow != null) {
if (fast.val != slow.val) return false;
fast = fast.next;
slow = slow.next;
}
return true;
}
/**
* 改进
*/
public boolean isPalindrome2(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
if (fast != null) slow = slow.next;
slow = reverse(slow);
while (slow != null && head.val == slow.val) {
head = head.next;
slow = slow.next;
}
return slow == null;
}
private ListNode reverse(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}