-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_linkedlist.py
More file actions
33 lines (27 loc) 路 911 Bytes
/
Copy pathbuild_linkedlist.py
File metadata and controls
33 lines (27 loc) 路 911 Bytes
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
class MyLinkedList:
def __init__(self):
self.List = []
def get(self, index: int) -> int:
if index > len(self.List) - 1:
return -1
return self.List[index]
def addAtHead(self, val: int) -> None:
self.List.insert(0, val)
def addAtTail(self, val: int) -> None:
self.List.append(val)
def addAtIndex(self, index: int, val: int) -> None:
if index == len(self.List):
self.addAtTail(val)
elif index < len(self.List):
self.List.insert(index, val)
def deleteAtIndex(self, index: int) -> None:
if index > len(self.List) - 1:
return None
self.List.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)