One Problem ask by my Faculty! Can Anyone Help? #25
-
Flatten a Multilevel Doubly Linked ListProb. Desc.You are given a doubly linked list, where each node contains:
The Write a function to flatten the list so that all nodes appear in a single-level doubly linked list. The flattened list should maintain the original order of nodes at each level. Constraints
ExampleInput:
Output:
Explanation: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Approach 1: Iterative Depth-First Search (DFS)Algorithm
Complexity
Code Implementation (Python)class Node:
def __init__(self, val, prev=None, next=None, child=None):
self.val = val
self.prev = prev
self.next = next
self.child = child
def flatten(head):
if not head:
return None
stack = []
current = head
while current:
if current.child:
# If there's a next node, push it to the stack
if current.next:
stack.append(current.next)
# Link child list to the current node
current.next = current.child
current.child.prev = current
current.child = None
if not current.next and stack:
# Pop the next node from the stack
next_node = stack.pop()
current.next = next_node
next_node.prev = current
current = current.next
return head
# Helper function to print
def print_list(head):
result = []
while head:
result.append(head.val)
head = head.next
print(result)
# Example usage
if __name__ == "__main__":
head = Node(1)
head.next = Node(2, head)
head.next.next = Node(3, head.next)
head.next.next.next = Node(4, head.next.next)
head.next.next.child = Node(7)
head.next.next.child.next = Node(8, head.next.next.child)
head.next.next.child.next.child = Node(11)
head.next.next.child.next.child.next = Node(12, head.next.next.child.next.child)
flattened = flatten(head)
print_list(flattened) Approach 2: Recursive Depth-First Search (DFS)Algorithm
Complexity
Code Implementation (Python)class Node:
def __init__(self, val, prev=None, next=None, child=None):
self.val = val
self.prev = prev
self.next = next
self.child = child
def flatten_recursively(head):
if not head:
return None
def dfs(node):
current = node
last = node
while current:
next_node = current.next
if current.child:
# Flatten the child list
child_head, child_tail = dfs(current.child)
# Connect the current node to the child list
current.next = child_head
child_head.prev = current
current.child = None
# Connect the tail of the child list to the next node
if next_node:
child_tail.next = next_node
next_node.prev = child_tail
last = child_tail
else:
last = current
current = next_node
return node, last
dfs(head)
return head
# Helper function to print
def print_list(head):
result = []
while head:
result.append(head.val)
head = head.next
print(result)
# Example usage
if __name__ == "__main__":
head = Node(1)
head.next = Node(2, head)
head.next.next = Node(3, head.next)
head.next.next.next = Node(4, head.next.next)
head.next.next.child = Node(7)
head.next.next.child.next = Node(8, head.next.next.child)
head.next.next.child.next.child = Node(11)
head.next.next.child.next.child.next = Node(12, head.next.next.child.next.child)
flattened = flatten_recursively(head)
print_list(flattened)
|
Approach | Time Complexity | Space Complexity | Notes |
---|---|---|---|
Iterative DFS | O(N) | O(N) | Uses a stack for tracking. Preferred for simplicity. |
Recursive DFS | O(N) | O(D) | Cleaner but may lead to stack overflow for deep nesting. |
Above Both the approaches are efficient and provide correct results. The iterative method is often preferred for practical use as it avoids potential recursion limits.
Beta Was this translation helpful? Give feedback.
Approach 1: Iterative Depth-First Search (DFS)
Algorithm
child
, push itsnext
node (if exists) onto the stack.child
list to the current node.child
pointer.Complexity
Code Implementation (Python)