Skip to content

Commit fd03eae

Browse files
committed
一刷707
1 parent 03a05bd commit fd03eae

File tree

9 files changed

+210
-44
lines changed

9 files changed

+210
-44
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,14 +4974,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
49744974
//|{doc_base_url}/0706-design-hashmap.adoc[题解]
49754975
//|Easy
49764976
//|
4977-
//
4978-
//|{counter:codes}
4979-
//|{leetcode_base_url}/design-linked-list/[707. Design Linked List^]
4980-
//|{source_base_url}/_0707_DesignLinkedList.java[Java]
4981-
//|{doc_base_url}/0707-design-linked-list.adoc[题解]
4982-
//|Medium
4983-
//|
4984-
//
4977+
4978+
|{counter:codes}
4979+
|{leetcode_base_url}/design-linked-list/[707. Design Linked List^]
4980+
|{source_base_url}/_0707_DesignLinkedList.java[Java]
4981+
|{doc_base_url}/0707-design-linked-list.adoc[题解]
4982+
|Medium
4983+
|
4984+
49854985
//|{counter:codes}
49864986
//|{leetcode_base_url}/insert-into-a-sorted-circular-linked-list/[708. Insert into a Sorted Circular Linked List^]
49874987
//|{source_base_url}/_0708_InsertIntoASortedCircularLinkedList.java[Java]

docs/0707-design-linked-list.adoc

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,83 @@
11
[#0707-design-linked-list]
2-
= 707. Design Linked List
2+
= 707. 设计链表
33

4-
{leetcode}/problems/design-linked-list/[LeetCode - Design Linked List^]
4+
https://leetcode.cn/problems/design-linked-list/[LeetCode - 707. 设计链表 ^]
55

6-
Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: `val` and `next`. `val` is the value of the current node, and `next` is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute `prev` to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
6+
你可以选择使用单链表或者双链表,设计并实现自己的链表。
77

8-
Implement these functions in your linked list class:
8+
单链表中的节点应该具备两个属性:`val``next``val` 是当前节点的值,`next` 是指向下一个节点的指针/引用。
99

10+
如果是双向链表,则还需要属性 `prev` 以指示链表中的上一个节点。假设链表中的所有节点下标从 *0* 开始。
1011

11-
* `get(index)` : Get the value of the `index`-th node in the linked list. If the index is invalid, return `-1`.
12-
* `addAtHead(val)` : 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.
13-
* `addAtTail(val)` : Append a node of value `val` to the last element of the linked list.
14-
* `addAtIndex(index, val)` : 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.
15-
* `deleteAtIndex(index)` : Delete the `index`-th node in the linked list, if the index is valid.
12+
实现 `MyLinkedList` 类:
1613

14+
* `MyLinkedList()` 初始化 `MyLinkedList` 对象。
15+
* `int get(int index)` 获取链表中下标为 `index` 的节点的值。如果下标无效,则返回 `-1`
16+
* `void addAtHead(int val)` 将一个值为 `val` 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
17+
* `void addAtTail(int val)` 将一个值为 `val` 的节点追加到链表中作为链表的最后一个元素。
18+
* `void addAtIndex(int index, int val)` 将一个值为 `val` 的节点插入到链表中下标为 `index` 的节点之前。如果 `index` 等于链表的长度,那么该节点会被追加到链表的末尾。如果 `index` 比长度更大,该节点将 *不会插入* 到链表中。
19+
* `void deleteAtIndex(int index)` 如果下标有效,则删除链表中下标为 `index` 的节点。
1720
18-
21+
*示例:*
1922

20-
*Example:*
23+
....
24+
输入
25+
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
26+
[[], [1], [3], [1, 2], [1], [1], [1]]
27+
输出
28+
[null, null, null, null, 2, null, 3]
29+
30+
解释
31+
MyLinkedList myLinkedList = new MyLinkedList();
32+
myLinkedList.addAtHead(1);
33+
myLinkedList.addAtTail(3);
34+
myLinkedList.addAtIndex(1, 2); // 链表变为 1->2->3
35+
myLinkedList.get(1); // 返回 2
36+
myLinkedList.deleteAtIndex(1); // 现在,链表变为 1->3
37+
myLinkedList.get(1); // 返回 3
38+
....
39+
40+
*提示:*
41+
42+
* `+0 <= index, val <= 1000+`
43+
* 请不要使用内置的 LinkedList 库。
44+
* 调用 `get``addAtHead``addAtTail``addAtIndex``deleteAtIndex` 的次数不超过 `2000`
2145
22-
[subs="verbatim,quotes,macros"]
23-
----
24-
*Input:*
25-
["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
26-
[[],[1],[3],[1,2],[1],[1],[1]]
27-
*Output:*
28-
[null,null,null,null,2,null,3]
29-
30-
*Explanation:*
31-
MyLinkedList linkedList = new MyLinkedList(); // Initialize empty LinkedList
32-
linkedList.addAtHead(1);
33-
linkedList.addAtTail(3);
34-
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
35-
linkedList.get(1); // returns 2
36-
linkedList.deleteAtIndex(1); // now the linked list is 1->3
37-
linkedList.get(1); // returns 3
38-
----
3946
40-
41-
*Constraints:*
47+
== 思路分析
4248

49+
image::images/0707-10.png[{image_attr}]
4350

44-
* `0 <= index,val <= 1000`
45-
* Please do not use the built-in LinkedList library.
46-
* At most `2000` calls will be made to `get`, `addAtHead`, `addAtTail`, `addAtIndex` and `deleteAtIndex`.
51+
image::images/0707-11.png[{image_attr}]
4752

53+
image::images/0707-12.png[{image_attr}]
4854

55+
image::images/0707-13.png[{image_attr}]
4956

5057

5158
[[src-0707]]
59+
[tabs]
60+
====
61+
一刷::
62+
+
63+
--
5264
[{java_src_attr}]
5365
----
5466
include::{sourcedir}/_0707_DesignLinkedList.java[tag=answer]
5567
----
68+
--
69+
70+
// 二刷::
71+
// +
72+
// --
73+
// [{java_src_attr}]
74+
// ----
75+
// include::{sourcedir}/_0707_DesignLinkedList_2.java[tag=answer]
76+
// ----
77+
// --
78+
====
79+
80+
81+
== 参考资料
82+
5683

docs/images/0707-10.png

42.6 KB
Loading

docs/images/0707-11.png

62 KB
Loading

docs/images/0707-12.png

71.2 KB
Loading

docs/images/0707-13.png

67.8 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ include::0704-binary-search.adoc[leveloffset=+1]
14961496

14971497
// include::0706-design-hashmap.adoc[leveloffset=+1]
14981498

1499-
// include::0707-design-linked-list.adoc[leveloffset=+1]
1499+
include::0707-design-linked-list.adoc[leveloffset=+1]
15001500

15011501
// include::0708-insert-into-a-sorted-circular-linked-list.adoc[leveloffset=+1]
15021502

logbook/202503.adoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,17 @@ endif::[]
423423
|{doc_base_url}/0721-accounts-merge.adoc[题解]
424424
|❌ 并查集。通过邮箱编号建立连接,而不是通过账户索引建立连接。
425425

426-
|{counter:codes}
427-
|{leetcode_base_url}/longest-consecutive-sequence/[128. Longest Consecutive Sequence^]
426+
|{counter:codes2503}
427+
|{leetcode_base_url}/longest-consecutive-sequence/[128. 最长连续序列^]
428428
|{doc_base_url}/0128-longest-consecutive-sequence.adoc[题解]
429429
|⭕️ 最初思路是排序。看答案,可以构建集合,然后“起点”(没有前一个数字)数字开始统计。
430430

431+
|{counter:codes}
432+
|{leetcode_base_url}/design-linked-list/[707. 设计链表^]
433+
|{doc_base_url}/0707-design-linked-list.adoc[题解]
434+
|✅
435+
436+
431437
|===
432438

433439
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0707_DesignLinkedList {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-22 14:50:41
9+
*/
10+
static class MyLinkedList {
11+
private final ListNode HEAD;
12+
private final ListNode TAIL;
13+
private int size;
14+
15+
public MyLinkedList() {
16+
HEAD = new ListNode(0);
17+
TAIL = new ListNode(0);
18+
HEAD.next = TAIL;
19+
TAIL.prev = HEAD;
20+
size = 0;
21+
}
22+
23+
public int get(int index) {
24+
ListNode node = getNode(index, false);
25+
if (node == null) {
26+
return -1;
27+
} else {
28+
return node.val;
29+
}
30+
}
31+
32+
public void addAtHead(int val) {
33+
addNode(HEAD, val);
34+
size++;
35+
}
36+
37+
public void addAtTail(int val) {
38+
addNode(TAIL.prev, val);
39+
size++;
40+
}
41+
42+
public void addAtIndex(int index, int val) {
43+
ListNode node = getNode(index, true);
44+
if (node != null) {
45+
addNode(node.prev, val);
46+
size++;
47+
}
48+
}
49+
50+
public void deleteAtIndex(int index) {
51+
ListNode node = getNode(index, false);
52+
if (node != null) {
53+
node.prev.next = node.next;
54+
node.next.prev = node.prev;
55+
size--;
56+
}
57+
}
58+
59+
private ListNode getNode(int index, boolean lager) {
60+
if (index < 0 || lager ? index > size : index >= size) {
61+
return null;
62+
}
63+
// 如果索引等于链表大小,则直接返回尾部
64+
if (index == size) {
65+
return TAIL;
66+
}
67+
ListNode cur = null;
68+
if (index > size / 2) {
69+
cur = TAIL.prev;
70+
for (int i = 0; i < size - index - 1; i++) {
71+
cur = cur.prev;
72+
}
73+
} else {
74+
cur = HEAD.next;
75+
for (int i = 0; i < index; i++) {
76+
cur = cur.next;
77+
}
78+
}
79+
return cur;
80+
}
81+
82+
private void addNode(ListNode prev, int val) {
83+
ListNode node = new ListNode(val);
84+
ListNode next = prev.next;
85+
node.next = next;
86+
node.prev = prev;
87+
prev.next = node;
88+
next.prev = node;
89+
}
90+
91+
private static class ListNode {
92+
public int val;
93+
public ListNode next;
94+
public ListNode prev;
95+
96+
public ListNode(int x) {
97+
val = x;
98+
}
99+
}
100+
}
101+
102+
// end::answer[]
103+
public static void main(String[] args) {
104+
MyLinkedList list = new MyLinkedList();
105+
// "addAtHead","addAtIndex","get","addAtHead","addAtTail","get","addAtTail","get","addAtHead","get","addAtHead"
106+
// [5], [1,2], [1], [6], [2], [3], [1], [5], [2], [2], [6]
107+
list.addAtHead(5);
108+
list.addAtIndex(1, 2);
109+
list.get(1);
110+
list.addAtHead(6);
111+
list.addAtTail(2);
112+
list.get(3);
113+
list.addAtTail(1);
114+
list.get(5);
115+
list.addAtHead(2);
116+
list.get(2);
117+
list.addAtHead(6);
118+
119+
// // "addAtHead","addAtHead","addAtHead","addAtIndex","deleteAtIndex","addAtHead","addAtTail","get","addAtHead","addAtIndex","addAtHead"
120+
// // [7], [2], [1], [3,0], [2], [6], [4], [4], [4], [5,0], [6]]
121+
// list.addAtHead(7);
122+
// list.addAtHead(2);
123+
// list.addAtHead(1);
124+
// list.addAtIndex(3, 0);
125+
// list.deleteAtIndex(2);
126+
// list.addAtHead(6);
127+
// list.addAtTail(4);
128+
// list.get(4);
129+
// list.addAtHead(4);
130+
// list.addAtIndex(5, 0);
131+
// list.addAtHead(6);
132+
}
133+
}

0 commit comments

Comments
 (0)