Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Copy List with Random Pointer problem solution

YASH PAL, 31 July 2024

In this Leetcode Copy List with Random Pointer problem solution A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random –> Y, then for the corresponding two nodes x and y in the copied list, x.random –> y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

  1. val: an integer representing Node.val
  2. random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.

Your code will only be given to the head of the original linked list.

Leetcode Copy List with Random Pointer problem solution

Problem solution in Python.

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        if not head:
            return None 
        check={}
        
        cur = head 
        while(cur):
            node = Node(cur.val, None, None)
            check[cur] = node
            cur = cur.next
            
        cur = head
        while(cur):
            if cur.next is not None:
                check[cur].next = check[cur.next]
            if cur.random is not None:
                check[cur].random = check[cur.random]
            cur = cur.next
            
        return check[head]

Problem solution in Java.

class Solution {

	Map<Node, Node> mappings = new HashMap<>();

	public Node getClone(Node oldNode) {

		if(oldNode != null) {

			if(!mappings.containsKey(oldNode)) {
				Node newNode = new Node(oldNode.val);
				this.mappings.put(oldNode, newNode);
			}

			return this.mappings.get(oldNode);
		}

		return null;

	}

	public Node copyRandomList(Node head) {

		if(head == null){
			return null;
		}

		Node oldHead = head;
		Node newHead = new Node(oldHead.val);
		mappings.put(oldHead, newHead);

		while(oldHead != null) {

			newHead = mappings.get(oldHead);

			newHead.next = this.getClone(oldHead.next);
			newHead.random = this.getClone(oldHead.random);

			newHead = newHead.next;
			oldHead = oldHead.next;


		}

		return this.mappings.get(head);
	}
}

Problem solution in C++.

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if (head == nullptr)
            return nullptr;
        Node* next, *temp, *curr;
        for(curr = head ; curr != nullptr ;){
            next = curr->next;
            curr->next = new Node(curr->val);
            curr->next->next = next;
            curr = next;
        }
        curr = head;
        while(curr != nullptr){
            curr->next->random = (curr->random != nullptr ? curr->random->next : nullptr);
            curr = curr->next->next;
        }
        Node* original = head, *copy = head->next;
        temp = copy;
        while(original && copy){
            original->next = (original->next != nullptr ? original->next->next : nullptr);
            copy->next = (copy->next != nullptr ? copy->next->next : nullptr);
            original = original->next;
            copy = copy->next;
        }
        return temp;
    }
};

Problem solution in C.

struct Node* copyRandomList(struct Node* head) {
	if (!head) {return NULL;}
	int index1 = 0, index2 = 0, index3;
	int* address1 = malloc(sizeof(int)*1024);
	int* address2 = malloc(sizeof(int)*1024);
	struct Node* temp1 = head;
	struct Node* copy = malloc(sizeof(struct Node));
	struct Node* temp2 = copy;

	while(temp1){
		temp2 -> val = temp1 -> val;
		address1[index1++] = temp1;
		address2[index2++] = temp2;
		if (temp1 -> next){
			struct Node* temp3 = malloc(sizeof(struct Node));
			temp2 -> next = temp3;
			temp2 = temp2 -> next;            
		 }
		temp1 = temp1 -> next;
	}
	address1[index1++] = NULL;
	address2[index2++] = NULL;
	temp2 -> next = NULL;

	temp1 = head;
	temp2 = copy;
	while(temp1){
		int temp4 = temp1 -> random;
		for (int i = 0; i < index1; i++){
			if (temp4 == address1[i]){
				index3 = i;
				break;
			}
		}
		temp2 -> random = (struct Node*) address2[index3];        
		temp1 = temp1 -> next;
		temp2 = temp2 -> next;
	}
	  return copy;
}

coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes