用于在 O(1) 空间中克隆具有 Next 和随机指针的链表的Java程序
给定一个链表,每个节点都有两个指针。第一个指向列表的下一个节点,但是,另一个指针是随机的,可以指向列表的任何节点。编写一个程序,在 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 并返回克隆列表
下面是实现。
Java
// Java program to clone a linked list with next
// and arbit pointers in O(n) time
class GfG {
// Structure of linked list Node
static class Node {
int data;
Node next, random;
Node(int x)
{
data = x;
next = random = null;
}
}
// Utility function to print the list.
static void print(Node start)
{
Node ptr = start;
while (ptr != null) {
System.out.println("Data = " + ptr.data
+ ", Random = "
+ ptr.random.data);
ptr = ptr.next;
}
}
// This function clones a given
// linked list in O(1) space
static Node clone(Node start)
{
Node curr = start, temp = null;
// insert additional node after
// every node of original list
while (curr != null) {
temp = curr.next;
// Inserting node
curr.next = new Node(curr.data);
curr.next.next = temp;
curr = temp;
}
curr = start;
// adjust the random pointers of the
// newly added nodes
while (curr != null) {
if (curr.next != null)
curr.next.random = (curr.random != null)
? curr.random.next
: curr.random;
// move to the next newly added node by
// skipping an original node
curr = curr.next.next;
}
Node original = start, copy = start.next;
// save the start of copied linked list
temp = copy;
// now separate the original list and copied list
while (original != null) {
original.next =original.next.next;
copy.next = (copy.next != null) ? copy.next.next
: copy.next;
original = original.next;
copy = copy.next;
}
return temp;
}
// Driver code
public static void main(String[] args)
{
Node start = new Node(1);
start.next = new Node(2);
start.next.next = new Node(3);
start.next.next.next = new Node(4);
start.next.next.next.next = new Node(5);
// 1's random points to 3
start.random = start.next.next;
// 2's random points to 1
start.next.random = start;
// 3's and 4's random points to 5
start.next.next.random = start.next.next.next.next;
start.next.next.next.random
= start.next.next.next.next;
// 5's random points to 2
start.next.next.next.next.random = start.next;
System.out.println("Original list : ");
print(start);
System.out.println("Cloned list : ");
Node cloned_list = clone(start);
print(cloned_list);
}
}
// This code is contributed by Prerna Saini.
输出
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 和随机指针克隆链接列表的完整文章!