-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathFlattening_a_Linked_List.py
48 lines (42 loc) · 1.31 KB
/
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
"""
Given a linked list where every node represents a linked list and contains two pointers of its type:
(i) Pointer to next node in the main list (we call it ‘right’ pointer in below code)
(ii) Pointer to a linked list where this node is head (we call it ‘down’ pointer in below code).
All linked lists are sorted. See the following example
```
5 -> 10 -> 19 -> 28
| | | |
V V V V
7 20 22 35
| | |
V V V
8 50 40
| |
V V
30 45
```
"""
class Node():
def __init__(self, data):
self.right = None
self.down = None
self.data = data
def flatten(node):
if not node or not node.right:
return node
return merge(node, flatten(node.right))
def merge(node1, node2):
if not node1:
return node2
if not node2:
return node1
if node1.data < node2.data:
head = node1
head.next = merge(node1.next, node2)
else:
head = node2
head.next = merge(node1, node2.next)
return head
# Notice:
# This is very qiao miao. Using recursion is like DFS, so will first flatten from the end.
# When we want to flatten the front part, the back parts are already flattened