Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 88a36ee

Browse files
Create Copy List with Random Pointer.cpp
1 parent d58bd3d commit 88a36ee

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Leet Code */
2+
/* Title - Copy List with Random Pointer */
3+
/* Created By - Akash Modak */
4+
/* Date - 18/06/2023 */
5+
6+
// 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.
7+
8+
// 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.
9+
10+
// 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.
11+
12+
// Return the head of the copied linked list.
13+
14+
// 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:
15+
16+
// val: an integer representing Node.val
17+
// 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.
18+
// Your code will only be given the head of the original linked list.
19+
20+
21+
22+
// Example 1:
23+
24+
25+
// Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
26+
// Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
27+
// Example 2:
28+
29+
30+
// Input: head = [[1,1],[2,1]]
31+
// Output: [[1,1],[2,1]]
32+
// Example 3:
33+
34+
35+
36+
// Input: head = [[3,null],[3,0],[3,null]]
37+
// Output: [[3,null],[3,0],[3,null]]
38+
39+
/*
40+
// Definition for a Node.
41+
class Node {
42+
public:
43+
int val;
44+
Node* next;
45+
Node* random;
46+
47+
Node(int _val) {
48+
val = _val;
49+
next = NULL;
50+
random = NULL;
51+
}
52+
};
53+
*/
54+
55+
class Solution {
56+
public:
57+
Node* copyRandomList(Node* head) {
58+
if(head==NULL) return head;
59+
unordered_map<Node*,Node*> mp;
60+
Node* temp = head;
61+
while(temp!=NULL){
62+
Node* n = new Node(temp->val);
63+
mp[temp]=n;
64+
temp=temp->next;
65+
}
66+
Node* newHead=NULL,*temp1;
67+
temp=head;
68+
while(temp!=NULL){
69+
if(newHead==NULL) {
70+
newHead=mp[temp];
71+
if(temp->random!=NULL) newHead->random=mp[temp->random];
72+
temp1=newHead;
73+
}
74+
else{
75+
temp1->next=mp[temp];
76+
temp1=temp1->next;
77+
if(temp->random!=NULL)
78+
temp1->random=mp[temp->random];
79+
}
80+
temp=temp->next;
81+
}
82+
return newHead;
83+
}
84+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /