-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyList.java
More file actions
103 lines (81 loc) · 2.2 KB
/
MyList.java
File metadata and controls
103 lines (81 loc) · 2.2 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
package com.list;
public class MyList {
/**
* head insert
*/
public static void headInsert(ListNode head,ListNode newNode){
ListNode old = head;
head = newNode;
head.next = old;
}
/**
* taild insert
*/
public static void tailInsert(ListNode tail,ListNode newNode){
ListNode old = tail;
tail = newNode;
old.next = newNode;
tail.next = null;
}
public static void travelList(ListNode head){
ListNode index = head;
while (index != null){
System.out.print(index.value + " ");
index = index.next;
}
System.out.println();
}
public static void insert(ListNode p,ListNode newNode){
ListNode old = p.next;
p.next = newNode;
newNode.next = old;
}
/**
* taild insert
*/
public static int find(ListNode list,int value){
int index = -1;
int count = 0;
while(list !=null){
if(list.value == value){
index = count;
return index;
}
count ++;
list = list.next;
}
return -1;
}
public static void delete(ListNode head,ListNode q){
if( q!=null && q.next != null){
ListNode p = q.next;
q.value = p.value;
q.next = p.next;
p = null;
}
if(q.next == null){
while(head != null){
if(head.next !=null && head.next == q){
head.next = null;
break;
}
}
}
}
public static void main(String[] args) {
ListNode listNode = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3= new ListNode(13);
listNode.next = listNode2;
listNode2.next = listNode3;
travelList(listNode);
headInsert(listNode,new ListNode(4));
travelList(listNode);
insert(listNode,new ListNode(5));
travelList(listNode);
tailInsert(listNode,new ListNode(6));
travelList(listNode);
delete(listNode,listNode2);
travelList(listNode);
}
}