LeetCode Offer 35. Copy of Complex Linked List

algo

链接:剑指 Offer 35. 复杂链表的复制 - 力扣(LeetCode)

做法:链表

相同:138. 复制带随机指针的链表

class Node:
  def __init__(self, x, next=None, random=None):
    self.val = int(x)
    self.next = next
    self.random = random
def copy_complex_linked_list(head):
  # create N' for N
  node = head
  while node:
    copy = Node(node.val, next=node.next, random=node.random)
    node.next = copy
    node = copy.next
  # move random links to node copies
  node = head
  while node:
    copy = node.next
    if copy.random:
      copy.random = copy.random.next
    node = copy.next
  # separate N' from N
  node = head
  while node:
    copy = node.next
    node_next = copy.next
    if copy.next:
      copy.next = copy.next.next
    node = node_next
  return head.next if head else None

Last update : May 28, 2023
Created : May 28, 2023