Advertisement
kosievdmerwe

Untitled

Oct 30th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. class Solution:
  2.     def flatten(self, head: 'Node') -> 'Node':
  3.         if head is None:
  4.             return None
  5.        
  6.         prev = None
  7.         to_process = [head]
  8.         while to_process:
  9.             cur = to_process.pop()
  10.             if cur.next:
  11.                 to_process.append(cur.next)
  12.             if cur.child:
  13.                 to_process.append(cur.child)
  14.                 cur.child = None
  15.            
  16.             cur.prev = prev
  17.             if prev:
  18.                 prev.next = cur
  19.             prev = cur
  20.        
  21.         return head
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement