-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkList.java
249 lines (231 loc) · 4.93 KB
/
LinkList.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
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
package com.lucien.algorithm;
public class LinkList {
class Node<T> {
public T data;
public Node next;
public Node(T data) {
this.data = data;
}
}
public Node head;
public Node current;
/**
* add node
* @param data
*/
private void add(Object data) {
//如果头节点为空,则创建一个新节点,并将其设置为头节点以及当前节点。
if(head == null) {
head = new Node(data);
current = head;
}
else {
current.next = new Node(data);
current = current.next;
}
}
/**
* insert node
* @param index insert index
* @param data
*/
private void insert(int index, Object data) {
current = head;
if(index == 0) {
head = new Node(data);
head.next = current;
return;
}
Node preNode = null;
while(current != null) {
if((int)current.data == index) {
Node node = new Node(data);
preNode.next = node;
node.next = current;
return;
}
preNode = current;
current = current.next;
}
}
/**
* delete node
* @param index delete index
*/
private void delete(int index) {
current = head;
int pos = 0;
Node preNode = head;
while(current != null) {
if(pos == index) {
if(pos == 0)
head = current.next;
//判断当前节点是否还有下一个节点,有则将前一个节点的next指向下一个节点,
//无则将前一个节点的next指向设置为null
else {
if(current.next != null) {
preNode.next = current.next;
}
else {
preNode.next = null;
}
}
return;
}
preNode = current;
current = current.next;
pos++;
}
}
/**
* 查找位置为index的节点,若无则返回为null
* @param index
* @return
*/
private Node find(int index) {
current = head;
int pos = 0;
while(current != null) {
if(index == pos) {
return current;
}
current = current.next;
pos++;
}
return null;
}
/**
* 获取链表的长度
* @return
*/
private int getLinkListLength() {
current = head;
int length = 0;
while(current != null) {
length++;
current = current.next;
}
return length;
}
/**
* print all the node data in linklist
* @param node
*/
private void print(Node node) {
if(node == null) {
return;
}
current = node;
while(current != null) {
System.out.print(current.data + "->");
current = current.next;
}
}
public Node reverseHead;
/**
* 链表反转
*/
private void reverseLinkList() {
current = head;
while(current != null) {
Node preHead = reverseHead;
reverseHead = current;
current = current.next;
if(preHead != null) {
reverseHead.next = preHead;
}
else {
reverseHead.next = null;
}
}
}
/**
* 链表反转(递归方式)
*/
private Node reverseLinkListRec(Node head) {
if(head == null || head.next == null)
return head;
Node newHead = reverseLinkListRec(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
/**
* 获取倒数第index节点
* @param index
* @return
*/
private Node getReciprocalNode(int index) {
if(index == 0)
return null;
current = head;
Node start = head;
while(current != null) {
index--;
if(index == 0)
break;
current = current.next;
}
// 长度大于链表长度
if(current == null) {
return null;
}
while(current.next != null) {
current = current.next;
start = start.next;
}
return start;
}
/**
* 获取链表的中间节点
* @return
*/
private Node getMiddleNode() {
Node oneStepNode = head;
Node twoStepNode = head;
//加入小于三个节点则返回第一个节点作为中间节点
if(head.next == null || head.next.next == null) {
return current;
}
while(twoStepNode.next != null && twoStepNode.next.next != null) {
twoStepNode = twoStepNode.next.next;
oneStepNode = oneStepNode.next;
}
return oneStepNode;
}
public static void main(String[] args) {
LinkList mLinkList = new LinkList();
for(int i = 0; i < 10; i++) {
mLinkList.add(i);
}
int index = 5;
//插入节点
mLinkList.insert(index, "insert");
//删除节点
mLinkList.delete(index);
//打印链表
mLinkList.print(mLinkList.head);
//查找第index个节点
Node findNode = mLinkList.find(index);
if(findNode != null) {
System.out.println(" data : " + findNode.data);
}
//打印链表的长度
System.out.println(" length : " + mLinkList.getLinkListLength());
//反转链表
mLinkList.reverseLinkList();
//打印反转链表
mLinkList.print(mLinkList.reverseHead);
System.out.println("");
//递归方式反转链表
mLinkList.print(mLinkList.reverseLinkListRec(mLinkList.reverseHead));
System.out.println("");
//获取倒数第index个节点
Node reciprocalNode = mLinkList.getReciprocalNode(index);
if(reciprocalNode != null) {
System.out.println("ReciprocalNode : " + reciprocalNode.data);
}
//获取链表中间节点
System.out.println("middle node data : " + mLinkList.getMiddleNode().data);
}
}