From 85f1f87c8dcb62daef1d1e94e81e115be96035bd Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:22:14 -0700 Subject: [PATCH] Update flatten_list.py --- linked_lists/flatten_list.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/linked_lists/flatten_list.py b/linked_lists/flatten_list.py index 278acdb..b4e7900 100644 --- a/linked_lists/flatten_list.py +++ b/linked_lists/flatten_list.py @@ -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