Linked Lists
61 / 170Linked ListsLesson 6 of 12About 41 min
When you must copy a structure whose links point anywhere, build the nodes first and the links second.
This tries to do it in one pass, creating each copy and wiring its jump link immediately. Type the two values it prints, separated by a single space.
class Node: def __init__(self, val): self.val, self.next, self.jump = val, None, None first, second = Node(1), Node(2)first.next = secondfirst.jump = second # forwardsecond.jump = first # backward copies = {}node = firstwhile node: copies[node] = Node(node.val) copies[node].jump = copies.get(node.jump) node = node.next print(copies[first].jump, copies[second].jump.val)Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.