-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flattening_a_Linked_List.py
48 lines (43 loc) · 1003 Bytes
/
Flattening_a_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
'''
class Node:
def __init__(self, d):
self.data=d
self.next=None
self.bottom=None
'''
# print list with next pointer
def flatten(root):
new = []
curr_n = head
curr_b = curr_n
while curr_n:
while curr_b:
new.append(curr_b.data)
curr_b = curr_b.bottom
curr_b = curr_n.next
curr_n = curr_n.next
new = sorted(new, reverse=True)
new_head = None
for i in new:
temp = Node(i)
temp.next = new_head
new_head = temp
return new_head
# print list with bottom pointer
def flatten(root):
new = []
curr_n = head
curr_b = curr_n
while curr_n:
while curr_b:
new.append(curr_b.data)
curr_b = curr_b.bottom
curr_b = curr_n.next
curr_n = curr_n.next
new = sorted(new, reverse=True)
new_head = None
for i in new:
temp = Node(i)
temp.bottom = new_head
new_head = temp
return new_head