-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path707.Design-Linked-List.java
171 lines (151 loc) · 3.95 KB
/
707.Design-Linked-List.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
// https://leetcode.com/problems/design-linked-list/
//
// algorithms
// Easy (20.84%)
// Total Accepted: 34,842
// Total Submissions: 167,186
// beats 100.0% of java submissions
class ListNode {
int val;
ListNode next;
ListNode prev;
ListNode(int val) {
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head;
ListNode tail;
/** Initialize your data structure here. */
public MyLinkedList() {
}
/**
* Get the value of the index-th node in the linked list. If the index is
* invalid, return -1.
*/
public int get(int index) {
ListNode n = getNode(index);
if (n == null) {
return -1;
}
return n.val;
}
public ListNode getNode(int index) {
if (index < 0 || index >= size) {
return null;
}
if (index <= size / 2) {
ListNode tmp = head;
for (int i = 0; i < index; i++) {
tmp = tmp.next;
}
return tmp;
}
ListNode tmp = tail;
for (int i = size - 1; i > index; i--) {
tmp = tmp.prev;
}
return tmp;
}
/**
* 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.
*/
public void addAtHead(int val) {
ListNode n = new ListNode(val);
n.next = head;
if (head != null) {
head.prev = n;
} else {
tail = n;
}
head = n;
size += 1;
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
ListNode n = new ListNode(val);
n.prev = tail;
if (tail != null) {
tail.next = n;
} else {
head = n;
}
tail = n;
size += 1;
}
public void deleteAtHead() {
if (size == 0) {
return;
}
head = head.next;
if (head != null) {
head.prev = null;
} else {
tail = null;
}
size -= 1;
}
public void deleteAtTail() {
if (size == 0) {
return;
}
tail = tail.prev;
if (tail != null) {
tail.next = null;
} else {
head = null;
}
size -= 1;
}
/**
* 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.
*/
public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
index = Math.max(index, 0);
if (index == 0) {
addAtHead(val);
} else if (index == size) {
addAtTail(val);
} else {
ListNode n = getNode(index);
ListNode newNode = new ListNode(val);
newNode.prev = n.prev;
n.prev.next = newNode;
newNode.next = n;
n.prev = newNode;
size++;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
ListNode n = getNode(index);
if (n == null) {
return;
}
if (index == 0) {
deleteAtHead();
} else if (index == size - 1) {
deleteAtTail();
} else {
ListNode prev = n.prev, next = n.next;
n = null;
prev.next = next;
next.prev = prev;
size -= 1;
}
}
}
/**
* 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);
*/