-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path707.py
209 lines (161 loc) · 5.96 KB
/
707.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
__________________________________________________________________________________________________
sample 84 ms submission
class MyLinkedList:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
def get(self, index: int) -> int:
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
"""
if index >= len(self.stack) or index < 0:
return -1
else:
return self.stack[index]
def addAtHead(self, val: int) -> None:
"""
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.
"""
self.stack.insert(0, val)
def addAtTail(self, val: int) -> None:
"""
Append a node of value val to the last element of the linked list.
"""
self.stack.append(val)
def addAtIndex(self, index: int, val: int) -> None:
"""
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.
"""
if len(self.stack) < index:
return -1
self.stack.insert(index, val)
def deleteAtIndex(self, index: int) -> None:
"""
Delete the index-th node in the linked list, if the index is valid.
"""
if index >= len(self.stack) or index < 0:
return -1
else:
self.stack.pop(index)
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
__________________________________________________________________________________________________
sample 13368 kb submission
class Node(object):
def __init__(self):
self.val = None
self.next = None
class MyLinkedList:
def __init__(self):
"""
Initialize your data structure here.
"""
self.count = 0
self.root = None
def get(self, index: int) -> int:
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
"""
if index >= self.count or index < 0:
return -1
head = self.root
for i in range(index):
head = head.next
# print(index, self.count, self.root)
return head.val
def addAtHead(self, val: int) -> None:
"""
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.
"""
node = Node()
node.val = val
node.next = self.root
self.root = node
self.count += 1
def addAtTail(self, val: int) -> None:
"""
Append a node of value val to the last element of the linked list.
"""
node = Node()
node.val = val
if self.count == 0:
self.root = node
else:
head = self.root
for _ in range(self.count-1):
head = head.next
head.next = node
self.count += 1
def addAtIndex(self, index: int, val: int) -> None:
"""
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.
"""
if index > self.count:
return
node = Node()
node.val = val
if self.count == 0:
node.next = self.root
self.root = node
self.count += 1
return
if self.count == index:
self.addAtTail(val)
else:
if index == 0:
self.addAtHead(val)
else:
head = self.root
for _ in range(index-1):
head = head.next
node.next = head.next
head.next = node
# node_before = self.getNode(index-1)
# node_after = self.getNode(index)
# new_node = Node()
# node.val = val
# node_before.next = new_node
# new_node.next = node_after
self.count += 1
def deleteAtIndex(self, index: int) -> None:
"""
Delete the index-th node in the linked list, if the index is valid.
"""
if index < 0 or index >= self.count:
return
node_to_delete = self.root
for _ in range(index):
node_to_delete = node_to_delete.next
del node_to_delete
if index == 0:
new_head = self.getNode(1)
self.root = new_head
else:
node_before = self.getNode(index-1)
node_after = self.getNode(index+1)
node_before.next = node_after
self.count -= 1
def getNode(self, index: int) -> ListNode:
cur = self.root
for _ in range(index):
cur = cur.next
return cur
# def get(self, index: 'int') -> 'int':
# if index < 0 or index >= self.count:
# return -1
# return self.getNode(index).val
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
__________________________________________________________________________________________________