Update flatten_list.py

This commit is contained in:
marina 2023-08-07 16:22:14 -07:00 committed by GitHub
parent ea12ccce74
commit 85f1f87c8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,14 +27,15 @@ def dfs(prev, node):
return dfs(last, temp_next)
def flatten(head):
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
sentinel = Node(None, None, head, None)
dfs(prev=sentinel, node=head)
# erase the pointer to sentinel and return
sentinel.next.prev = None
return sentinel.next