From 5b5e9ebb364b80128a013fcae0991cd1d673d081 Mon Sep 17 00:00:00 2001 From: Larissa Lages Date: Sun, 19 Mar 2023 22:38:45 -0400 Subject: [PATCH] add deep_copy_list question --- others/deep_copy_list.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 others/deep_copy_list.py diff --git a/others/deep_copy_list.py b/others/deep_copy_list.py new file mode 100644 index 0000000..af73acc --- /dev/null +++ b/others/deep_copy_list.py @@ -0,0 +1,32 @@ +def deep_copy_list(head): + c = head + new_head = None + ap = {} + + # create new list + while c: + n = Node(c.value) + ap[c] = n + n.arbitrary = c.arbitrary # point to arbitrary of older list + + if not new_head: + new_head = n + pn = new_head + + else: + pn.next = n + pn = pn.next + + c = c.next + + # update arbitrary pointer in new list + c = new_head + while c: + c.arbitrary = ap[a.arbitrary] + c = c.next + + return new_head + + + +