-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.py
43 lines (33 loc) · 1.12 KB
/
SinglyLinkedList.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
class MyLinkedList:
def __init__(self):
self.head = Node(0) # dummy node
self.size = 0
def get(self, index: int) -> int:
if index >= self.size: # invalid index
return -1
cur = self.head
for _ in range(index + 1):
cur = cur.next
return cur.val
def addAtHead(self, val: int) -> None:
self.addAtIndex(0, val)
def addAtTail(self, val: int) -> None:
self.addAtIndex(self.size, val)
def addAtIndex(self, index: int, val: int) -> None:
if index > self.size: # invalid index
return
prev = self.head
for _ in range(index):
prev = prev.next
# Add newNode between [prev] and [prev.next]
newNode = Node(val, prev.next)
prev.next = newNode
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index >= self.size: # invalid index
return
prev = self.head
for _ in range(index):
prev = prev.next
prev.next = prev.next.next
self.size -= 1