-
Notifications
You must be signed in to change notification settings - Fork 32
/
SingleLinkSample.java
53 lines (48 loc) · 1.4 KB
/
SingleLinkSample.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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 演示单链表的使用
*/
public class SingleLinkSample{
public static void main(String[] args) {
Node head = new Node(null,null);
Node first = new Node(2,null);
Node second = new Node(5,null);
first.next = second;
head.next = first;
// 注意顺序,①把first也链接在insert后,②再把insert链接在head后
// 因为通常情况下我们只持有head实例的引用
Node insert = new Node(7,null);
insert.next = head.next;
head.next = insert;
// 删除数据2
Node delete = head.next;
Node prev = delete;
while(delete!=null){
if(delete.data == 2){
// 也要注意顺序,先把后边的数据,挂载在前一数据后边
prev.next = delete.next;
// 再删除数据2
delete.next = null;
break;
}
// 记录前一数据
prev = delete;
delete = delete.next;
}
Node print = head.next;
while (print!=null) {
System.out.println(print.data);
print = print.next;
}
}
}
class Node{
Integer data;
Node next;
public Node(Integer data, Node next){
this.data = data;
this.next = next;
}
}