|
1 | 1 | class MyLinkedList {
|
2 | 2 |
|
3 | 3 | /** Initialize your data structure here. */
|
4 |
| - List<Integer> list; |
| 4 | + Node head; |
| 5 | + Node tail; |
| 6 | + int count; |
5 | 7 | public MyLinkedList() {
|
6 |
| - list = new ArrayList<>(); |
| 8 | + head = new Node(-1); |
| 9 | + tail = new Node(-1); |
| 10 | + head.next = tail; |
| 11 | + tail.prev = head; |
| 12 | + count = 0; |
7 | 13 | }
|
8 | 14 |
|
9 | 15 | /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
10 | 16 | public int get(int index) {
|
11 |
| - if (index >= list.size() || index < 0) { |
| 17 | + if (index < 0 || index >= count) { |
12 | 18 | return -1;
|
13 | 19 | }
|
14 |
| - |
15 |
| - return list.get(index); |
| 20 | + Node curr = head.next; |
| 21 | + int idx = 0; |
| 22 | + while (idx < index) { |
| 23 | + idx++; |
| 24 | + curr = curr.next; |
| 25 | + } |
| 26 | + return curr.val; |
16 | 27 | }
|
17 | 28 |
|
18 | 29 | /** 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. */
|
19 | 30 | public void addAtHead(int val) {
|
20 |
| - list.add(0, val); |
| 31 | + Node newNode = new Node(val); |
| 32 | + newNode.next = head.next; |
| 33 | + newNode.prev = head; |
| 34 | + head.next.prev = newNode; |
| 35 | + head.next = newNode; |
| 36 | + count++; |
21 | 37 | }
|
22 | 38 |
|
23 | 39 | /** Append a node of value val to the last element of the linked list. */
|
24 | 40 | public void addAtTail(int val) {
|
25 |
| - list.add(val); |
| 41 | + Node newNode = new Node(val); |
| 42 | + newNode.prev = tail.prev; |
| 43 | + newNode.next = tail; |
| 44 | + tail.prev.next = newNode; |
| 45 | + tail.prev = newNode; |
| 46 | + count++; |
26 | 47 | }
|
27 | 48 |
|
28 | 49 | /** 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. */
|
29 | 50 | public void addAtIndex(int index, int val) {
|
30 |
| - if (index > list.size()) { |
| 51 | + if (index < 0 || index > count) { |
31 | 52 | return;
|
32 | 53 | }
|
33 |
| - if (index == list.size()) { |
| 54 | + if (index == 0) { |
| 55 | + addAtHead(val); |
| 56 | + } |
| 57 | + else if (index == count) { |
34 | 58 | addAtTail(val);
|
35 | 59 | }
|
36 | 60 | else {
|
37 |
| - list.add(index, val); |
| 61 | + int idx = 0; |
| 62 | + Node curr = head; |
| 63 | + while (idx < index) { |
| 64 | + idx++; |
| 65 | + curr = curr.next; |
| 66 | + } |
| 67 | + Node newNode = new Node(val); |
| 68 | + newNode.prev = curr; |
| 69 | + newNode.next = curr.next; |
| 70 | + curr.next.prev = newNode; |
| 71 | + curr.next = newNode; |
| 72 | + count++; |
38 | 73 | }
|
39 | 74 | }
|
40 | 75 |
|
41 | 76 | /** Delete the index-th node in the linked list, if the index is valid. */
|
42 | 77 | public void deleteAtIndex(int index) {
|
43 |
| - if (index >= 0 && index < list.size()) { |
44 |
| - list.remove(index); |
| 78 | + if (index < 0 || index >= count) { |
| 79 | + return; |
| 80 | + } |
| 81 | + int idx = 0; |
| 82 | + Node curr = head; |
| 83 | + while (idx < index) { |
| 84 | + idx++; |
| 85 | + curr = curr.next; |
45 | 86 | }
|
| 87 | + Node next = curr.next.next; |
| 88 | + next.prev = curr; |
| 89 | + curr.next = next; |
| 90 | + count--; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +class Node { |
| 95 | + int val; |
| 96 | + Node next; |
| 97 | + Node prev; |
| 98 | + |
| 99 | + public Node(int val) { |
| 100 | + this.val = val; |
| 101 | + next = null; |
| 102 | + prev = null; |
46 | 103 | }
|
47 | 104 | }
|
48 | 105 |
|
|
0 commit comments