-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
73 lines (65 loc) · 1.27 KB
/
Node.java
File metadata and controls
73 lines (65 loc) · 1.27 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
package algorithm.list;
/**
* @ClassName Node
* @Description 单链表
* @Author changxuan
* @Date 2020/6/23 下午7:33
**/
public class Node {
public int data;
public Node next = null;
public Node(int data){
this.data = data;
}
/**
* 尾插
* @param d 元素
*/
void appendToTail(int d){
Node end = new Node(d);
Node n = this;
while (n.next != null){
n = n.next;
}
n.next = end;
}
/**
* 头插
* @param d 元素
* @return 链表头
*/
Node appendToHead(int d){
Node end = new Node(d);
end.next = this;
return end;
}
/**
* 打印元素
*/
void print(){
Node n = this;
do {
System.out.println(n.data);
n = n.next;
}while (n != null);
}
/**
* 删除链表第一个某元素
* @param d 元素
* @return 表头
*/
Node deleteNode(int d){
Node n = this;
if (n.data == d){
return this.next;
}
while (n.next != null){
if (n.next.data == d){
n.next = n.next.next;
return this;
}
n = n.next;
}
return this;
}
}