-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0707-design-linked-list.py
67 lines (54 loc) · 1.82 KB
/
0707-design-linked-list.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
class ListNode:
def __init__(self, val):
self.val = val
self.prev = None
self.next = None
class MyLinkedList:
def __init__(self):
self.left = ListNode(0)
self.right = ListNode(0)
self.left.next = self.right
self.right.prev = self.left
def get(self, index: int) -> int:
cur = self.left.next
while cur and index > 0:
cur = cur.next
index -= 1
if cur and cur != self.right and index == 0:
return cur.val
return -1
def addAtHead(self, val: int) -> None:
node, prev, next = ListNode(val), self.left, self.left.next
node.next, node.prev = next, prev
next.prev = node
prev.next = node
def addAtTail(self, val: int) -> None:
node, prev, next = ListNode(val), self.right.prev, self.right
node.next, node.prev = next, prev
next.prev = node
prev.next = node
def addAtIndex(self, index: int, val: int) -> None:
next = self.left.next
while next and index > 0:
next = next.next
index -= 1
if next and index == 0:
node, prev = ListNode(val), next.prev
node.next, node.prev = next, prev
next.prev = node
prev.next = node
def deleteAtIndex(self, index: int) -> None:
node = self.left.next
while node and index > 0:
node = node.next
index -= 1
if node and node != self.right and index == 0:
node.prev.next = node.next
node.next.prev = node.prev
# 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)