mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create flatten_list.py
This commit is contained in:
parent
86d533c4bf
commit
18389003c4
40
linked_lists/flatten_list.py
Normal file
40
linked_lists/flatten_list.py
Normal file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, val, prev, next, child):
|
||||
self.val = val
|
||||
self.prev = prev
|
||||
self.next = next
|
||||
self.child = child
|
||||
|
||||
|
||||
def dfs(prev, node):
|
||||
|
||||
if not node:
|
||||
return prev
|
||||
|
||||
node.prev = prev
|
||||
prev.next = node
|
||||
temp_next = node.next
|
||||
|
||||
last = dfs(node, node.child)
|
||||
node.child = None
|
||||
|
||||
return dfs(last, temp_next)
|
||||
|
||||
|
||||
def flatten(head):
|
||||
|
||||
if head is None:
|
||||
return head
|
||||
|
||||
pseudo_head = Node(None, None, head, None)
|
||||
|
||||
dfs(pseudo_head, head)
|
||||
pseudo_head.next.prev = None
|
||||
|
||||
return pseudo_head.next
|
Loading…
x
Reference in New Issue
Block a user