-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPalindrome.java
More file actions
80 lines (74 loc) · 1.9 KB
/
IsPalindrome.java
File metadata and controls
80 lines (74 loc) · 1.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
package class01;
import java.util.Stack;
/**
* @author pacai
* @version 1.0
* 回文链表
*/
public class IsPalindrome {
public static class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
}
}
//need O(n) extra space
public<T> boolean isPalindrome1(Node<T> head) {
Stack<Node<T>> stack = new Stack<>();
Node<T> cur = head;
while(cur != null){
stack.push(cur);
cur = cur.next;
}
while(!stack.isEmpty()){
if(stack.pop().data != head.data){
return false;
}
head = head.next;
}
return true;
}
//need O(1) extra space
public static<T> boolean isPalindrome2(Node<T> head) {
if(head == null || head.next == null){
return true;
}
Node<T> n1 = head;
Node<T> n2 = head;
while(n1.next != null && n2.next.next != null){
n1 = n1.next;
n2 = n2.next.next;
}
n2 = n1.next; //n2为中点的下一个结点
n1.next = null;//将中点的next指针置为空
Node<T> n3 = null;
while(n2 != null){ //反转链表
n3 = n2.next;
n2.next = n1;
n1 = n2;
n2 = n3;
}
n3 = n1;
n2 = head;
boolean res = true;
while(n1 != null && n2 != null){
if(n1.data != n2.data){
res = false;
break;
}
n1 = n1.next;
n2 = n2.next;
}
//最后还要把链表恢复
n1 = n3.next;
n3.next = null;
while(n1 != null){
n2 = n1.next;
n1.next = n3;
n3 = n1;
n1 = n2;
}
return res;
}
}