You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: problems/0707.设计链表.md
+101Lines changed: 101 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -158,6 +158,7 @@ private:
158
158
159
159
Java:
160
160
```Java
161
+
//单链表
161
162
class ListNode {
162
163
int val;
163
164
ListNode next;
@@ -236,6 +237,106 @@ class MyLinkedList {
236
237
pred.next = pred.next.next;
237
238
}
238
239
}
240
+
241
+
//双链表
242
+
class MyLinkedList {
243
+
class ListNode {
244
+
int val;
245
+
ListNode next,prev;
246
+
ListNode(int x) {val = x;}
247
+
}
248
+
249
+
int size;
250
+
ListNode head,tail;//Sentinel node
251
+
252
+
/** Initialize your data structure here. */
253
+
public MyLinkedList() {
254
+
size = 0;
255
+
head = new ListNode(0);
256
+
tail = new ListNode(0);
257
+
head.next = tail;
258
+
tail.prev = head;
259
+
}
260
+
261
+
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
262
+
public int get(int index) {
263
+
if(index < 0 || index >= size){return -1;}
264
+
ListNode cur = head;
265
+
266
+
// 通过判断 index < (size - 1) / 2 来决定是从头结点还是尾节点遍历,提高效率
267
+
if(index < (size - 1) / 2){
268
+
for(int i = 0; i <= index; i++){
269
+
cur = cur.next;
270
+
}
271
+
}else{
272
+
cur = tail;
273
+
for(int i = 0; i <= size - index - 1; i++){
274
+
cur = cur.prev;
275
+
}
276
+
}
277
+
return cur.val;
278
+
}
279
+
280
+
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
281
+
public void addAtHead(int val) {
282
+
ListNode cur = head;
283
+
ListNode newNode = new ListNode(val);
284
+
newNode.next = cur.next;
285
+
cur.next.prev = newNode;
286
+
cur.next = newNode;
287
+
newNode.prev = cur;
288
+
size++;
289
+
}
290
+
291
+
/** Append a node of value val to the last element of the linked list. */
292
+
public void addAtTail(int val) {
293
+
ListNode cur = tail;
294
+
ListNode newNode = new ListNode(val);
295
+
newNode.next = tail;
296
+
newNode.prev = cur.prev;
297
+
cur.prev.next = newNode;
298
+
cur.prev = newNode;
299
+
size++;
300
+
}
301
+
302
+
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
303
+
public void addAtIndex(int index, int val) {
304
+
if(index > size){return;}
305
+
if(index < 0){index = 0;}
306
+
ListNode cur = head;
307
+
for(int i = 0; i < index; i++){
308
+
cur = cur.next;
309
+
}
310
+
ListNode newNode = new ListNode(val);
311
+
newNode.next = cur.next;
312
+
cur.next.prev = newNode;
313
+
newNode.prev = cur;
314
+
cur.next = newNode;
315
+
size++;
316
+
}
317
+
318
+
/** Delete the index-th node in the linked list, if the index is valid. */
319
+
public void deleteAtIndex(int index) {
320
+
if(index >= size || index < 0){return;}
321
+
ListNode cur = head;
322
+
for(int i = 0; i < index; i++){
323
+
cur = cur.next;
324
+
}
325
+
cur.next.next.prev = cur;
326
+
cur.next = cur.next.next;
327
+
size--;
328
+
}
329
+
}
330
+
331
+
/**
332
+
* Your MyLinkedList object will be instantiated and called as such:
0 commit comments