-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-Recursive.py
More file actions
30 lines (28 loc) · 1.07 KB
/
12-Recursive.py
File metadata and controls
30 lines (28 loc) · 1.07 KB
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
class Node:
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
def util(self, head): # Does most of the work
curr = head
tail = head
while curr: # Iterate through the list
follow = curr.next
kid = curr.child
if kid: # If child exists, find its tail
kidTail = self.util(kid)
kidTail.next = follow # Connect child tail to main list
if follow: follow.prev = kidTail
curr.next = kid
kid.prev = curr
curr.child = None # Now, the child is no longer in a different branch
else:
curr = curr.next
if curr:
tail = curr
return tail # For child tail recursion. Not needed for main flattening
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if head:
self.util(head)
return head