Skip to content

Commit f69e6ba

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0707
1 parent 326168d commit f69e6ba

File tree

6 files changed

+440
-44
lines changed

6 files changed

+440
-44
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
1. [奇偶链表](/solution/0300-0399/0328.Odd%20Even%20Linked%20List/README.md)
7373
1. [环形链表](/solution/0100-0199/0141.Linked%20List%20Cycle/README.md)
7474
1. [环形链表 II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README.md)
75+
1. [设计链表](/solution/0700-0799/0707.Design%20Linked%20List/README.md)
7576

7677
### 二叉树
7778

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
7070
1. [Odd Even Linked List](/solution/0300-0399/0328.Odd%20Even%20Linked%20List/README_EN.md)
7171
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)
7272
1. [Linked List Cycle II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README_EN.md)
73+
1. [Design Linked List](/solution/0700-0799/0707.Design%20Linked%20List/README_EN.md)
7374

7475
### Binary Tree
7576

solution/0700-0799/0707.Design Linked List/README.md

+164-2
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,184 @@ linkedList.get(1); //返回3
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
定义虚拟头结点 dummy,count 记录当前链表结点个数。
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
5052

5153
<!-- 这里可写当前语言的特殊实现逻辑 -->
5254

5355
```python
54-
56+
class ListNode:
57+
58+
def __init__(self, val=0, next=None):
59+
self.val = val
60+
self.next = next
61+
62+
63+
class MyLinkedList:
64+
65+
def __init__(self):
66+
"""
67+
Initialize your data structure here.
68+
"""
69+
self.dummy = ListNode()
70+
self.count = 0
71+
72+
73+
def get(self, index: int) -> int:
74+
"""
75+
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
76+
"""
77+
if index < 0 or index >= self.count:
78+
return -1
79+
cur = self.dummy.next
80+
for _ in range(index):
81+
cur = cur.next
82+
return cur.val
83+
84+
85+
def addAtHead(self, val: int) -> None:
86+
"""
87+
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.
88+
"""
89+
self.addAtIndex(0, val)
90+
91+
92+
def addAtTail(self, val: int) -> None:
93+
"""
94+
Append a node of value val to the last element of the linked list.
95+
"""
96+
self.addAtIndex(self.count, val)
97+
98+
99+
def addAtIndex(self, index: int, val: int) -> None:
100+
"""
101+
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.
102+
"""
103+
if index > self.count:
104+
return
105+
pre = self.dummy
106+
for _ in range(index):
107+
pre = pre.next
108+
pre.next = ListNode(val, pre.next)
109+
self.count += 1
110+
111+
112+
def deleteAtIndex(self, index: int) -> None:
113+
"""
114+
Delete the index-th node in the linked list, if the index is valid.
115+
"""
116+
if index < 0 or index >= self.count:
117+
return
118+
pre = self.dummy
119+
for _ in range(index):
120+
pre = pre.next
121+
t = pre.next
122+
pre.next = t.next
123+
t.next = None
124+
self.count -= 1
125+
126+
127+
128+
# Your MyLinkedList object will be instantiated and called as such:
129+
# obj = MyLinkedList()
130+
# param_1 = obj.get(index)
131+
# obj.addAtHead(val)
132+
# obj.addAtTail(val)
133+
# obj.addAtIndex(index,val)
134+
# obj.deleteAtIndex(index)
55135
```
56136

57137
### **Java**
58138

59139
<!-- 这里可写当前语言的特殊实现逻辑 -->
60140

61141
```java
62-
142+
class MyLinkedList {
143+
144+
private class ListNode {
145+
int val;
146+
ListNode next;
147+
ListNode(int val) {
148+
this(val, null);
149+
}
150+
ListNode(int val, ListNode next) {
151+
this.val = val;
152+
this.next = next;
153+
}
154+
}
155+
156+
private ListNode dummy;
157+
private int count;
158+
159+
/** Initialize your data structure here. */
160+
public MyLinkedList() {
161+
dummy = new ListNode(0);
162+
count = 0;
163+
}
164+
165+
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
166+
public int get(int index) {
167+
if (index < 0 || index >= count) {
168+
return -1;
169+
}
170+
ListNode cur = dummy.next;
171+
while (index-- > 0) {
172+
cur = cur.next;
173+
}
174+
return cur.val;
175+
}
176+
177+
/** 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. */
178+
public void addAtHead(int val) {
179+
addAtIndex(0, val);
180+
}
181+
182+
/** Append a node of value val to the last element of the linked list. */
183+
public void addAtTail(int val) {
184+
addAtIndex(count, val);
185+
}
186+
187+
/** 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. */
188+
public void addAtIndex(int index, int val) {
189+
if (index > count) {
190+
return;
191+
}
192+
ListNode pre = dummy;
193+
while (index-- > 0) {
194+
pre = pre.next;
195+
}
196+
pre.next = new ListNode(val, pre.next);
197+
++count;
198+
}
199+
200+
/** Delete the index-th node in the linked list, if the index is valid. */
201+
public void deleteAtIndex(int index) {
202+
if (index < 0 || index >= count) {
203+
return;
204+
}
205+
ListNode pre = dummy;
206+
while (index-- > 0) {
207+
pre = pre.next;
208+
}
209+
ListNode t = pre.next;
210+
pre.next = t.next;
211+
t.next = null;
212+
--count;
213+
}
214+
}
215+
216+
/**
217+
* Your MyLinkedList object will be instantiated and called as such:
218+
* MyLinkedList obj = new MyLinkedList();
219+
* int param_1 = obj.get(index);
220+
* obj.addAtHead(val);
221+
* obj.addAtTail(val);
222+
* obj.addAtIndex(index,val);
223+
* obj.deleteAtIndex(index);
224+
*/
63225
```
64226

65227
### **...**

solution/0700-0799/0707.Design Linked List/README_EN.md

+162-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,173 @@ linkedList.get(1);&nbsp;&nbsp;&nbsp; // returns 3
5353
### **Python3**
5454

5555
```python
56-
56+
class ListNode:
57+
58+
def __init__(self, val=0, next=None):
59+
self.val = val
60+
self.next = next
61+
62+
63+
class MyLinkedList:
64+
65+
def __init__(self):
66+
"""
67+
Initialize your data structure here.
68+
"""
69+
self.dummy = ListNode()
70+
self.count = 0
71+
72+
73+
def get(self, index: int) -> int:
74+
"""
75+
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
76+
"""
77+
if index < 0 or index >= self.count:
78+
return -1
79+
cur = self.dummy.next
80+
for _ in range(index):
81+
cur = cur.next
82+
return cur.val
83+
84+
85+
def addAtHead(self, val: int) -> None:
86+
"""
87+
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.
88+
"""
89+
self.addAtIndex(0, val)
90+
91+
92+
def addAtTail(self, val: int) -> None:
93+
"""
94+
Append a node of value val to the last element of the linked list.
95+
"""
96+
self.addAtIndex(self.count, val)
97+
98+
99+
def addAtIndex(self, index: int, val: int) -> None:
100+
"""
101+
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.
102+
"""
103+
if index > self.count:
104+
return
105+
pre = self.dummy
106+
for _ in range(index):
107+
pre = pre.next
108+
pre.next = ListNode(val, pre.next)
109+
self.count += 1
110+
111+
112+
def deleteAtIndex(self, index: int) -> None:
113+
"""
114+
Delete the index-th node in the linked list, if the index is valid.
115+
"""
116+
if index < 0 or index >= self.count:
117+
return
118+
pre = self.dummy
119+
for _ in range(index):
120+
pre = pre.next
121+
t = pre.next
122+
pre.next = t.next
123+
t.next = None
124+
self.count -= 1
125+
126+
127+
128+
# Your MyLinkedList object will be instantiated and called as such:
129+
# obj = MyLinkedList()
130+
# param_1 = obj.get(index)
131+
# obj.addAtHead(val)
132+
# obj.addAtTail(val)
133+
# obj.addAtIndex(index,val)
134+
# obj.deleteAtIndex(index)
57135
```
58136

59137
### **Java**
60138

61139
```java
62-
140+
class MyLinkedList {
141+
142+
private class ListNode {
143+
int val;
144+
ListNode next;
145+
ListNode(int val) {
146+
this(val, null);
147+
}
148+
ListNode(int val, ListNode next) {
149+
this.val = val;
150+
this.next = next;
151+
}
152+
}
153+
154+
private ListNode dummy;
155+
private int count;
156+
157+
/** Initialize your data structure here. */
158+
public MyLinkedList() {
159+
dummy = new ListNode(0);
160+
count = 0;
161+
}
162+
163+
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
164+
public int get(int index) {
165+
if (index < 0 || index >= count) {
166+
return -1;
167+
}
168+
ListNode cur = dummy.next;
169+
while (index-- > 0) {
170+
cur = cur.next;
171+
}
172+
return cur.val;
173+
}
174+
175+
/** 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. */
176+
public void addAtHead(int val) {
177+
addAtIndex(0, val);
178+
}
179+
180+
/** Append a node of value val to the last element of the linked list. */
181+
public void addAtTail(int val) {
182+
addAtIndex(count, val);
183+
}
184+
185+
/** 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. */
186+
public void addAtIndex(int index, int val) {
187+
if (index > count) {
188+
return;
189+
}
190+
ListNode pre = dummy;
191+
while (index-- > 0) {
192+
pre = pre.next;
193+
}
194+
pre.next = new ListNode(val, pre.next);
195+
++count;
196+
}
197+
198+
/** Delete the index-th node in the linked list, if the index is valid. */
199+
public void deleteAtIndex(int index) {
200+
if (index < 0 || index >= count) {
201+
return;
202+
}
203+
ListNode pre = dummy;
204+
while (index-- > 0) {
205+
pre = pre.next;
206+
}
207+
ListNode t = pre.next;
208+
pre.next = t.next;
209+
t.next = null;
210+
--count;
211+
}
212+
}
213+
214+
/**
215+
* Your MyLinkedList object will be instantiated and called as such:
216+
* MyLinkedList obj = new MyLinkedList();
217+
* int param_1 = obj.get(index);
218+
* obj.addAtHead(val);
219+
* obj.addAtTail(val);
220+
* obj.addAtIndex(index,val);
221+
* obj.deleteAtIndex(index);
222+
*/
63223
```
64224

65225
### **...**

0 commit comments

Comments
 (0)