コード例 #1
0
 def creating_list() -> Node:
     # initiating nodes with data
     n1, n2, n3, n4 = Node(5), Node(6), Node(7), Node(8)
     # connecting each node with its Successor
     n1.next, n2.next, n3.next, n4.next = n2, n3, n4, None
     # connecting each node with its random node
     n1.rnd = n3
     n2.rnd = n4
     n3.rnd = n2
     n4.rnd = n1
     # returning linked list head node
     return n1
コード例 #2
0
def duplicate_1(head):
    dic = dict()
    dic[None] = None
    p = head

    while p:
        dic[p] = Node(p.val)
        p = p.next

    p = head
    while p:
        dic[p].next = dic[p.next]
        dic[p].random = dic[p.random]
        p = p.next

    return dic[head]