-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
117 lines (92 loc) · 2.53 KB
/
LinkedList.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
public class LinkedList {
public static class Node {
public int data;
public Node next;
public Node(){}
public Node(int data, Node next){
this.data = data;
this.next = next;
}
}
private Node head = null;
// 在P节点后添加X O(1)
public void inset(Node p, Node x){
if(p == null){
x.next = head;
head = x;
} else {
x.next = p.next;
p.next = x;
}
}
// 已知前驱节点 O(1)
public void remove(Node p, Node x){
if(p == null){
head = head.next;
} else {
p.next = x.next;
}
}
// 未知前驱节点,O(n)
public void remove(Node x){
if(x == null) return;
Node p = head;
while(p !=null && p.next != x){
p = p.next;
}
if(p == null){
head = head.next;
} else {
p.next = x.next;
}
}
// 值查找节点
public Node find(int val){
Node p = head;
while(p != null && p.data != val){
p = p.next;
}
return p;
}
// 翻转链表
public void revert(){
}
@Override
public String toString(){
String s = "LinkedList: ";
Node p = head;
while(p != null){
s += p.data + "->";
p = p.next;
}
return s;
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
LinkedList.Node node = new LinkedList.Node();
node.data = 1;
linkedList.inset(null, node);
LinkedList.Node node2 = new LinkedList.Node();
node2.data = 2;
linkedList.inset(node, node2);
LinkedList.Node node3 = new LinkedList.Node();
node3.data = 3;
linkedList.inset(node2, node3);
System.out.println(linkedList.toString());
LinkedList.Node node10 = new LinkedList.Node();
node10.data = 10;
linkedList.inset(node, node10);
LinkedList.Node node99 = new LinkedList.Node();
node99.data = 99;
linkedList.inset(null, node99);
System.out.println(linkedList.toString());
linkedList.remove(node99);
System.out.println(linkedList.toString());
linkedList.remove(node10);
System.out.println(linkedList.toString());
Node n = linkedList.find(3);
System.out.println(n);
Node n2 = linkedList.find(9);
System.out.println(n2);
}
}