📌  相关文章
📜  用于在 O(1) 空间中克隆具有 Next 和随机指针的链表的Python程序

📅  最后修改于: 2022-05-13 01:56:56.547000             🧑  作者: Mango

用于在 O(1) 空间中克隆具有 Next 和随机指针的链表的Python程序

给定一个链表,每个节点都有两个指针。第一个指向列表的下一个节点,但是,另一个指针是随机的,可以指向列表的任何节点。编写一个程序,在 O(1) 空间中克隆给定列表,即没有任何额外空间。
例子:

Input : Head of the below-linked list

Output :
A new linked list identical to the original list.

在之前的帖子中,Set-1 和 Set-2 讨论了各种方法,并且还提供了 O(n) 空间复杂度实现。
在这篇文章中,我们将实现一个不需要额外空间的算法,如 Set-1 中所讨论的。
下面是算法:

  • 创建节点 1 的副本并将其插入到原始链表中的节点 1 和节点 2 之间,创建 2 的副本并将其插入到 2 和 3 之间。以这种方式继续,在第 N 个节点之后添加 N 的副本
  • 现在以这种方式复制随机链接
original->next->random= original->random->next;  /*TRAVERSE 
TWO NODES*/
  • 这是有效的,因为 original->next 只不过是原始的副本,而 Original->random->next 只不过是随机的副本。
  • 现在以这种方式在一个循环中恢复原始和复制链表。
original->next = original->next->next;
     copy->next = copy->next->next;
  • 确保 original->next 为 NULL 并返回克隆列表

下面是实现。

Python
'''Python program to clone a linked list with next and arbitrary pointers'''
'''Done in O(n) time with O(1) extra space'''
  
class Node:
    '''Structure of linked list node'''
  
    def __init__(self, data):
        self.data = data
        self.next = None
        self.random = None
  
def clone(original_root):
    '''Clone a doubly linked list with random pointer'''
    '''with O(1) extra space'''
  
    '''Insert additional node after every node of original list'''
    curr = original_root
    while curr != None:
        new = Node(curr.data)
        new.next = curr.next
        curr.next = new
        curr = curr.next.next
  
    '''Adjust the random pointers of the newly added nodes'''
    curr = original_root
    while curr != None:
        curr.next.random = curr.random.next
        curr = curr.next.next
  
    '''Detach original and duplicate list from each other'''
    curr = original_root
    dup_root = original_root.next
    while curr.next != None:
        tmp = curr.next
        curr.next = curr.next.next
        curr = tmp
  
    return dup_root
  
def print_dlist(root):
    '''Function to print the doubly linked list'''
  
    curr = root
    while curr != None:
        print('Data =', curr.data, ', Random =', curr.random.data)
        curr = curr.next
  
####### Driver code #######
'''Create a doubly linked list'''
original_list = Node(1)
original_list.next = Node(2)
original_list.next.next = Node(3)
original_list.next.next.next = Node(4)
original_list.next.next.next.next = Node(5)
  
'''Set the random pointers'''
  
# 1's random points to 3
original_list.random = original_list.next.next
  
# 2's random points to 1
original_list.next.random = original_list
  
# 3's random points to 5
original_list.next.next.random = original_list.next.next.next.next
  
# 4's random points to 5
original_list.next.next.next.random = original_list.next.next.next.next
  
# 5's random points to 2
original_list.next.next.next.next.random = original_list.next
  
'''Print the original linked list'''
print('Original list:')
print_dlist(original_list)
  
'''Create a duplicate linked list'''
cloned_list = clone(original_list)
  
'''Print the duplicate linked list'''
print('
Cloned list:')
print_dlist(cloned_list)
  
'''This code is contributed by Shashank Singh'''


输出
Original list : 
Data = 1, Random  = 3
Data = 2, Random  = 1
Data = 3, Random  = 5
Data = 4, Random  = 5
Data = 5, Random  = 2

Cloned list : 
Data = 1, Random  = 3
Data = 2, Random  = 1
Data = 3, Random  = 5
Data = 4, Random  = 5
Data = 5, Random  = 2

有关更多详细信息,请参阅有关在 O(1) 空间中使用 next 和随机指针克隆链接列表的完整文章!