-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly.py
54 lines (35 loc) · 1.02 KB
/
doubly.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
class DoublyLinked:
def __init__(self, value, next=None, prev=None):
self.value = value
self.next = next
self.prev = prev
def __str__(self):
return str(self.value)
head = tail = DoublyLinked(1)
# Display O(n)
def display(head):
curr = head
elements = []
while curr:
elements.append(str(curr.value))
curr = curr.next
print(" <-> ".join(elements))
display(head)
# Insertion at the start O(1)
def insert_at_beg(val, head, tail):
new_node = DoublyLinked(val, next=head)
head.prev = new_node
return new_node, tail
head, tail = insert_at_beg(2, head, tail)
display(head)
# Insert at the end O(1)
def insert_at_end(val, head, tail):
new_node = DoublyLinked(val, prev=tail)
tail.next = new_node
return head, new_node
head, tail = insert_at_end(3, head, tail)
display(head)
head, tail = insert_at_beg(10, head, tail)
display(head)
head, tail = insert_at_end(20, head, tail)
display(head)