-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-linked-list.py
75 lines (59 loc) · 1.85 KB
/
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
68
69
70
71
72
73
74
75
class Node:
def __init__(self, data = 0):
self.data = data
self.next = None
self.prev = None
class MyLinkedList:
def __init__(self):
self.dum = Node()
def get(self, indx: int) -> int:
temp = self.dum
for _ in range(indx + 1):
if temp:
temp = temp.next
if temp:
return temp.data
return -1
def addAtHead(self, val: int) -> None:
new_head = Node(val)
old_head = self.dum.next
new_head.next = old_head
if old_head:
old_head.prev = new_head
self.dum.next = new_head
new_head.prev = self.dum
def addAtTail(self, val: int) -> None:
old_tail = self.dum
while old_tail and old_tail.next:
old_tail = old_tail.next
new_tail = Node(val)
if old_tail:
old_tail.next = new_tail
new_tail.prev = old_tail
def addAtIndex(self, indx: int, val: int) -> None:
prv = self.dum
for _ in range(indx):
if prv:
prv = prv.next
if prv and prv.next:
new_node = Node(val)
new_node.next = prv.next
new_node.next.prev = new_node
prv.next = new_node
new_node.prev = prv
if prv and not prv.next:
self.addAtTail(val)
def deleteAtIndex(self, indx: int) -> None:
temp = self.dum
for _ in range(indx):
if temp:
temp = temp.next
if temp and temp.next:
temp.next = temp.next.next
# 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)