Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2c03dfc

Browse files
feat(0707): 新增java 双链表 版本
1 parent 2c24d81 commit 2c03dfc

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

‎problems/0707.设计链表.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private:
158158
159159
Java:
160160
```Java
161+
//单链表
161162
class ListNode {
162163
int val;
163164
ListNode next;
@@ -236,6 +237,106 @@ class MyLinkedList {
236237
pred.next = pred.next.next;
237238
}
238239
}
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:
333+
* MyLinkedList obj = new MyLinkedList();
334+
* int param_1 = obj.get(index);
335+
* obj.addAtHead(val);
336+
* obj.addAtTail(val);
337+
* obj.addAtIndex(index,val);
338+
* obj.deleteAtIndex(index);
339+
*/
239340
```
240341

241342
Python:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /