diff --git a/src/main/java/com/thealgorithms/datastructures/lists/Copy_List_With_Random_Pointer.java b/src/main/java/com/thealgorithms/datastructures/lists/Copy_List_With_Random_Pointer.java new file mode 100644 index 000000000000..54e1e885f81d --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/Copy_List_With_Random_Pointer.java @@ -0,0 +1,54 @@ +package com.thealgorithms.datastructures.lists; + +class Node { + int val; + Node next; + Node random; + + public Node(int val) { + this.val = val; + this.next = null; + this.random = null; + } +} + +public class Solution { + public Node copyRandomList(Node head) { + if (head == null) { + return null; + } + + // Step 1: Clone each node and insert it next to the original node + Node current = head; + while (current != null) { + Node clone = new Node(current.val); + clone.next = current.next; + current.next = clone; + current = clone.next; + } + + // Step 2: Set the random pointers for the cloned nodes + current = head; + while (current != null) { + if (current.random != null) { + current.next.random = current.random.next; + } + current = current.next.next; + } + + // Step 3: Separate the original and cloned lists + Node original = head, copy = head.next; + Node clonedHead = head.next; + while (original != null) { + original.next = original.next.next; + if (copy.next != null) { + copy.next = copy.next.next; + } + original = original.next; + copy = copy.next; + } + + return clonedHead; + } +} +