-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM13.java
More file actions
103 lines (97 loc) · 2.95 KB
/
BM13.java
File metadata and controls
103 lines (97 loc) · 2.95 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package NiukeTOP101;
import java.util.Stack;
public class BM13 {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
if(head == null || head.next == null){
return true;
}
ListNode left = head, mid = head.next, right = head.next.next;
while(right != null && right.next != null){
left = left.next;
mid = mid.next;
right = right.next.next;
}
left.next = null;
ListNode newHead = reverseList(mid);
while (head != null && newHead != null){
if(head.val == newHead.val){
head = head.next;
newHead = newHead.next;
}else{
return false;
}
}
return true;
}
private ListNode reverseList(ListNode mid) {
ListNode pre = null, cur = mid, nextCur;
while (cur != null){
nextCur = cur.next;
cur.next = pre;
pre = cur;
cur = nextCur;
}
return pre;
}
/**
* 头插法
* @param mid
* @return
*/
private ListNode insertHead(ListNode mid){
ListNode dummy = new ListNode(-1);
dummy.next = mid;
ListNode pre = dummy, cur = mid, nextCur;
while (cur.next != null){
nextCur = cur.next;
cur.next = nextCur.next;
nextCur.next = pre.next;
pre.next = nextCur;
}
return dummy.next;
}
public static void main(String[] args) {
BM13 bm2 = new BM13();
ListNode nodeSta = new ListNode(0); //创建首节点
ListNode nextNode; //声明一个变量用来在移动过程中指向当前节点
nextNode=nodeSta; //指向首节点
//这句话就相当于 ListNode nextNode = nodeSta = new ListNode(0);
//创建链表
int[] x = new int[]{1,2,2,1};
for(int i=0;i<x.length;i++){
ListNode node = new ListNode(x[i]); //生成新的节点
nextNode.next=node; //把新节点连起来
nextNode=nextNode.next; //当前节点往后移动
} //当for循环完成之后 nextNode指向最后一个节点,
nextNode=nodeSta; //重新赋值让它指向首节点
bm2.isPail(nextNode.next);
}
}
/**
* 栈逆序
*/
class BM13_1{
public boolean isPail (ListNode head) {
ListNode p = head;
Stack<Integer> s = new Stack<>();
// 辅助栈记录数据
while (p != null){
s.push(p.val);
p = p.next;
}
p = head;
// 正序遍历链表,从栈中弹出的内容是逆序的
while (!s.isEmpty()){
// 比较是否相同
if(p.val != s.pop())
return false;
p = p.next;
}
return true;
}
}