Dashboard Temp Share Shortlinks Frames API

HTMLify

flattening-a-linked-list.py
Views: 140 | Author: prakhardoneria
 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
class Solution:
    def merge(self, a, b):
        if not a: return b
        if not b: return a
        
        result = None
        
        if a.data < b.data:
            result = a
            result.bottom = self.merge(a.bottom, b)
        else:
            result = b
            result.bottom = self.merge(a, b.bottom)
            
        result.next = None
        return result

    def flatten(self, root):
        if not root or not root.next:
            return root
        
        root.next = self.flatten(root.next)
        root = self.merge(root, root.next)
        
        return root