-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm707 DLL.java
173 lines (146 loc) · 3.84 KB
/
m707 DLL.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
class MyLinkedList {
private class Node {
public int val;
public Node next;
public Node prev;
public Node(int val, Node next, Node prev) {
this.val = val;
this.next = next;
this.prev = prev;
}
public Node(int val, Node prev) {
this(val, null, prev);
}
public Node(int val) {
this(val, null);
}
}
public Node head;
public Node tail;
public int size;
public MyLinkedList() {
this.size = 0;
}
public int get(int index) {
if (head == null || index >= size || index < 0) {
return -1;
}
Node current;
if (index > size / 2) {
current = tail;
for (int i = size - 1; i > index; i--) {
current = current.prev;
}
} else {
current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
}
return current.val;
}
public void addAtHead(int val) {
if (head == null) {
head = new Node(val);
tail = head;
} else {
Node newHead = new Node(val);
newHead.next = head;
head.prev = newHead;
head = newHead;
}
size++;
}
public void addAtTail(int val) {
if (tail == null) {
head = new Node(val);
tail = head;
} else {
Node newTail = new Node(val);
newTail.prev = tail;
tail.next = newTail;
tail = newTail;
}
size++;
}
public void addAtIndex(int index, int val) {
if (index > size || index < 0) {
return;
}
if (index == 0) {
addAtHead(val);
return;
}
if (index == size) {
addAtTail(val);
return;
}
Node current;
if (index > size / 2) {
current = tail;
for (int i = size - 1; i > index - 1; i--) {
current = current.prev;
}
} else {
current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
}
Node newNode = new Node(val, current.next, current);
current.next.prev = newNode;
current.next = newNode;
size++;
}
public void deleteAtIndex(int index) {
if (index >= size || index < 0) {
return;
}
if (index == 0) {
if (size == 1) {
head = null;
tail = null;
size--;
return;
}
head = head.next;
head.prev = null;
size--;
return;
}
if (index == size - 1) {
tail = tail.prev;
tail.next = null;
size--;
return;
}
Node current;
if (index > size / 2) {
current = tail;
for (int i = size - 1; i > index - 1; i--) {
current = current.prev;
}
current.next = current.next.next;
if (current.next != null) {
current.next.prev = current;
}
} else {
current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
current.next.prev = current;
}
size--;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/