-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleWithHeadLinkedList.java
More file actions
318 lines (292 loc) · 8.55 KB
/
Copy pathSingleWithHeadLinkedList.java
File metadata and controls
318 lines (292 loc) · 8.55 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package Array;
import Node.Node;
import sun.plugin2.applet.context.NoopExecutionContext;
/**
* 单链表反转链
* 表中环的检测
* 两个有序的链表合并
* 删除链表倒数第 n 个结点
* 求链表的中间结点
*/
/**
* 特殊头结点-没有值的头结点
* Node head = new Node(null,null);
* pro: 不用区分插入删除的结点位置。begin middle last
* cause: 因为自带null值头,所以不需要改变头部的指向,插入删除时。
* */
public class SingleWithHeadLinkedList<T> implements _LinkedList<T> {
public Node<T> head;
public Node<T> tail;
public SingleWithHeadLinkedList() {
this.head = new Node<T>(null,null); ;
}
public SingleWithHeadLinkedList(Node<T> head) {
this();//have head
this.head.next = tail.next = head;//头尾确定好head
tail = tail.next;//更新尾巴指向。最后一个才是尾巴。
}
public SingleWithHeadLinkedList(T[] arr) {
this();//have head
if (arr != null && arr.length > 0) {
this.head.next = new Node<>(arr[0]);//赋第一个元素
tail = this.head.next;//赋好尾巴
int i = 1;
while (i < arr.length) {
tail.next = new Node<>(arr[i++]);
tail = tail.next;
}
}
}
public SingleWithHeadLinkedList(SingleWithHeadLinkedList<T> list) {
this();
if (list != null && list.head.next != null) {
this.head.next = new Node<>(list.head.data);
tail = this.head.next;
Node<T> node = list.head.next;
while (node != null) {
tail.next = new Node<>(node.data);
tail = tail.next;
node = node.next;
}
}
}
/**
* 快慢指针,检测环是否存在
*/
public static boolean hasLoop1(Node node){
if (node == null) {
return false;
}
Node slow = node;
Node fast = node;
while (fast != null && fast.next != null){
//遍历一个节点
slow = slow.next;
//遍历两个节点
fast = fast.next.next;
}
//到链表末尾
if(fast ==null){
return false;
} else if (slow == fast) {
//指针相遇
return true;
}
return false;
}
/**
* 递归反转法,从尾结点开始,逆向反转各个结点的指针域指向。
*/
private static Node linkedListReverseRecur(Node current){
// current看作是前一结点,current.getNext()是当前结点,reHead是反转后新链表的头结点
if (current == null || current.next == null) {
return current;
}
Node temp = current.next;
Node newHead = linkedListReverseRecur(current.next);
temp.next = current;
current.next = null;
return newHead;
}
private static Node linkedListReverseList(Node node){
Node pre = null;
Node next = null;
while (node != null){
next = node.next;
node.next = pre;
pre = node;
node = next;
}
return pre;
}
private static Node linkedListReverseList2(Node node){
Node pre = null;
Node cur = node;
while (cur != null){
Node nextNode = cur.next;
cur.next = pre;
pre = cur;
cur = nextNode;
}
return pre;
}
private static Node reverseList(Node node){
if (node == null || node.next == null)
return node;
Node pre = null;
Node cur = node;
Node next = null;
while (cur != null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
@Override
public boolean isEmpty() {
return this.head.next == null;
}
@Override
public int length() {
int length = 0;
//已经有头
Node<T> cNode = head.next;
while (cNode != null) {
length ++;
cNode = cNode.next;
}
return length;
}
@Override
public T get(int index) {
if (index >= 0) {
int j = 0;
Node<T> node = this.head.next;
while (node != null && j < index) {
node = node.next;
j++;
}
if (node != null) {
return node.data;
}
}
return null;
}
@Override
public T set(int index, T data) {
if (index >= 0 && data != null) {
Node<T> node = this.head.next;
int j = 0;
while (node != null){
node = node.next;
j++;
}
if (node != null) {
T old = node.data;
node.data = data;
return old;
}
}
return null;
}
@Override
public boolean add(int index, T data) {
/*头部插入,中间插入,尾部插入*/
if (data == null) {
throw new NullPointerException("");
}
if (index < 0) {
throw new NullPointerException("");
}
int j = 0;
Node<T> pre = this.head;
//find before index one
//已有headNode 所以不用index-1
while (pre.next != null && j < index ) {
pre = pre.next;
j ++ ;
}
//新插入,并修改指针指向
Node<T> one = new Node<T>(data,pre.next);
pre.next = one;
//如果是末尾指针,把最后指针指给新加入的node
if (pre == tail) {
this.tail = one;
}
return true;
}
@Override
public boolean add(T data) {
if (data == null) {
throw new NullPointerException();
}
this.tail.next = new Node<>(data);
this.tail = this.tail.next;
return true;
}
@Override
public T remove(int index) {
T old = null;
if (index >= 0) {
Node<T> node = this.head;
int j = 0;
while (node != null && j < index) {
node = node.next;
j++;
}
Node<T> current = node.next;
if (current != null) {
old = current.data;
//如果是最后一个
if (current == this.tail) {
this.tail = current;
}
//前节点的指针,指向当前节点的指针后面。
node.next = current.next;
}
}
return old;
}
/*
* 删除所有包含data值的节点
* */
@Override
public boolean removeAll(T data) {
boolean remove = false;
if (data != null) {
//node前一个,front是当前节点。
Node<T> node = this.head;
Node<T> front = node.next;
while (front != null) {
if (front.data.equals(data)) {
//如果是尾巴,则继续更新尾巴
if (tail.data.equals(data)) {
this.tail = front;
}
//前节点指针
node.next = front.next;
front = node.next;
remove = true;
} else {
//指针链接上
//?想了半天,应该就是把node 和 front 重置。重新链上。
node = front;
front = front.next;
}
}
}
return remove;
}
@Override
public void clear() {
this.head.next = null;
this.tail = this.head;
}
@Override
public boolean contains(T data) {
if (data != null) {
Node<T> pre = this.head.next;
while (pre != null) {
if (data.equals(pre.data)) {
return true;
}
pre = pre.next;
}
}
return false;
}
public void concat(SingleWithHeadLinkedList<T> list) {
if (this.head.next == null) {
this.head.next = list.head.next;
} else {
Node<T> node = this.head.next;
while (node != null) {
node = node.next;
}
node.next = list.head.next;//最后节点赋给另外list的头(自带头)的下一个
this.tail = list.tail;//整个尾巴是list的尾巴
}
}
}