forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeLinkedList.java
63 lines (59 loc) · 1.63 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
package com.haobin.leetcode.linkedlist;
import com.haobin.leetcode.linkedlist.ReverseList.ListNode;
import java.util.List;
import java.util.Stack;
/**
* @Author HaoBin
* @Create 2020/1/16 15:26
* @Description: 回文链表
**/
public class PalindromeLinkedList {
/**
* 全部压入栈,然后出栈与原链表比较
*/
public boolean isPalindrome1(ListNode head) {
Stack<Integer> stack = new Stack<>();
ListNode stackPointer = head;
while (stackPointer != null) {
stack.push(stackPointer.val);
stackPointer = stackPointer.next;
}
while (stack.empty()) {
if (head.val != stack.pop()) {
return false;
}
head = head.next;
}
return true;
}
/**
* 快慢指针
* 翻转前半部分链表,然后跟后半部分比较
*/
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) {
pre = slow;
slow = slow.next;
fast = fast.next.next;
pre.next = prepre;
prepre = pre;
}
if(fast != null) {
slow = slow.next;
}
while(pre != null && slow != null) {
if(pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
}