Skip to content

Commit d9b3f38

Browse files
authored
Update Design Linked List.java
1 parent 8d6afe6 commit d9b3f38

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

Easy/Design Linked List.java

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,105 @@
11
class MyLinkedList {
22

33
/** Initialize your data structure here. */
4-
List<Integer> list;
4+
Node head;
5+
Node tail;
6+
int count;
57
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;
713
}
814

915
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
1016
public int get(int index) {
11-
if (index >= list.size() || index < 0) {
17+
if (index < 0 || index >= count) {
1218
return -1;
1319
}
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;
1627
}
1728

1829
/** 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. */
1930
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++;
2137
}
2238

2339
/** Append a node of value val to the last element of the linked list. */
2440
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++;
2647
}
2748

2849
/** 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. */
2950
public void addAtIndex(int index, int val) {
30-
if (index > list.size()) {
51+
if (index < 0 || index > count) {
3152
return;
3253
}
33-
if (index == list.size()) {
54+
if (index == 0) {
55+
addAtHead(val);
56+
}
57+
else if (index == count) {
3458
addAtTail(val);
3559
}
3660
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++;
3873
}
3974
}
4075

4176
/** Delete the index-th node in the linked list, if the index is valid. */
4277
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;
4586
}
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;
46103
}
47104
}
48105

0 commit comments

Comments
 (0)