-
Notifications
You must be signed in to change notification settings - Fork 0
/
36_Flatten_LL.py
50 lines (30 loc) · 866 Bytes
/
36_Flatten_LL.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
class Node:
def __init__(self, val=0, next=None, child=None):
self.data = val
self.next = next
self.child = child
# Don't change the code above.
def merge_2_LL(a:Node, b:Node) -> Node:
if b == None:
return a
temp = Node()
result = temp
while(a and b):
if a.data < b.data:
temp.child = a
a = a.child
temp = temp.child
else:
temp.child = b
b = b.child
temp = temp.child
if a: temp.child = a
if b: temp.child = b
return result.child
def flattenLinkedList(head: Node) -> Node:
# Write your code here
if head == None or head.next == None:
return head
head.next = flattenLinkedList(head.next)
head = merge_2_LL(head,head.next)
return head