Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Copy List with Random Pointer #753

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Java/Linked List/Copy List with Random Pointer
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Author: Jawakar Sri
// Date Created: 01/10/2024
// Title: LeetCode Problem - Copy List with Random Pointer
// Problem Link: https://leetcode.com/problems/copy-list-with-random-pointer/

// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}

// Solution 1: Using extra space (HashMap)
class SolutionUsingHashMap {
public Node copyRandomList(Node head) {
if (head == null) return null;

Node curr = head, newHead = new Node(0);
Node newCurr = newHead;
HashMap<Node, Node> map = new HashMap();

// First pass: Create new nodes and store them in the map
while (curr != null) {
if (!map.containsKey(curr)) {
newCurr.next = new Node(curr.val);
map.put(curr, newCurr.next);
} else {
newCurr.next = map.get(curr);
}

newCurr = newCurr.next;

// Create random pointers
if (curr.random != null) {
if (!map.containsKey(curr.random)) {
newCurr.random = new Node(curr.random.val);
map.put(curr.random, newCurr.random);
} else {
newCurr.random = map.get(curr.random);
}
}

curr = curr.next;
}

return newHead.next;
}
}

// Solution 2: Constant space (In-place modification)
class SolutionUsingInPlace {
public Node copyRandomList(Node head) {
if (head == null) return null;

Node curr = head;

// Step 1: Create a new node for each original node and link them in place
while (curr != null) {
Node next = curr.next;
curr.next = new Node(curr.val);
curr.next.next = next;
curr = next;
}

// Step 2: Set random pointers for the copied nodes
curr = head;
while (curr != null) {
if (curr.random != null) {
curr.next.random = curr.random.next;
}
curr = curr.next.next;
}

// Step 3: Separate the original and copied lists
curr = head;
Node newHead = new Node(0); // Dummy node
Node newCurr = newHead;

while (curr != null) {
Node next = curr.next.next;
newCurr.next = curr.next;
newCurr = newCurr.next;
curr.next = next;
curr = next;
}

return newHead.next;
}
}