`{"id":"f3446c2b-0824-4293-b331-b59d3999dd8a","name":"Copy Linkedlist With Random Pointers","description":"\r\n1. Given a singly linklist with an additional random pointer which could point to any node in the list or NULL.\r\n2. Return a deep copy of the list.\r\n3. For More Details watch Video.","inputFormat":"input is handle for you\r\n","outputFormat":"output is handle for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n ListNode *random = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *copyRandomList(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n\r\n vector<ListNode *> arr(n);\r\n ListNode *prev = nullptr;\r\n for (int i = 0; i < n; i++)\r\n {\r\n arr[i] = new ListNode(0);\r\n if (prev != nullptr)\r\n prev->next = arr[i];\r\n prev = arr[i];\r\n }\r\n for (int i = 0; i < n; i++)\r\n {\r\n int val, idx;\r\n cin >> val >> idx;\r\n\r\n arr[i]->val = val;\r\n if (idx != -1)\r\n arr[i]->random = arr[idx];\r\n }\r\n\r\n ListNode *head = copyRandomList(arr[0]);\r\n while (head != nullptr)\r\n {\r\n cout << \"(\" + to_string(head->val) + \", \" + (head->random != nullptr ? to_string(head->random->val) : to_string(-1)) + \") \";\r\n head = head->next;\r\n }\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n ListNode random = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode copyRandomList(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n ListNode[] arr = new ListNode[n];\r\n ListNode prev = null;\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = new ListNode(0);\r\n if (prev != null)\r\n prev.next = arr[i];\r\n prev = arr[i];\r\n }\r\n\r\n for (int i = 0; i < n; i++) {\r\n int val = scn.nextInt();\r\n int idx = scn.nextInt();\r\n\r\n arr[i].val = val;\r\n if(idx != -1) arr[i].random = arr[idx];\r\n }\r\n\r\n ListNode head = copyRandomList(arr[0]);\r\n while (head != null) {\r\n System.out.print(\"(\" + head.val + \", \" + (head.random != null ? head.random.val : -1) + \") \");\r\n head = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7 -1\r\n15 0\r\n18 4\r\n10 2\r\n5 0","sampleOutput":"(7, -1) (15, 7) (18, 5) (10, 18) (5, 7) ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"747f2850-e8c6-421d-a657-f37a19750523","name":"Copy Linkedlist With Random Pointers","slug":"copy-linkedlist-with-random-pointers","type":1}],"next":{"id":"da5ebaf3-96f0-4b9c-a552-b72fa3c9358e","name":"Copy Linkedlist With Random Pointers MCQ","type":0,"slug":"copy-linkedlist-with-random-pointers-mcq"},"prev":{"id":"c0cb1b8c-b117-42a0-ab54-45ce3bbc0334","name":"Reverse In Range","type":3,"slug":"reverse-in-range"}}}`

# Copy Linkedlist With Random Pointers

1. Given a singly linklist with an additional random pointer which could point to any node in the list or NULL. 2. Return a deep copy of the list. 3. For More Details watch Video.

`{"id":"f3446c2b-0824-4293-b331-b59d3999dd8a","name":"Copy Linkedlist With Random Pointers","description":"\r\n1. Given a singly linklist with an additional random pointer which could point to any node in the list or NULL.\r\n2. Return a deep copy of the list.\r\n3. For More Details watch Video.","inputFormat":"input is handle for you\r\n","outputFormat":"output is handle for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n ListNode *random = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *copyRandomList(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n\r\n vector<ListNode *> arr(n);\r\n ListNode *prev = nullptr;\r\n for (int i = 0; i < n; i++)\r\n {\r\n arr[i] = new ListNode(0);\r\n if (prev != nullptr)\r\n prev->next = arr[i];\r\n prev = arr[i];\r\n }\r\n for (int i = 0; i < n; i++)\r\n {\r\n int val, idx;\r\n cin >> val >> idx;\r\n\r\n arr[i]->val = val;\r\n if (idx != -1)\r\n arr[i]->random = arr[idx];\r\n }\r\n\r\n ListNode *head = copyRandomList(arr[0]);\r\n while (head != nullptr)\r\n {\r\n cout << \"(\" + to_string(head->val) + \", \" + (head->random != nullptr ? to_string(head->random->val) : to_string(-1)) + \") \";\r\n head = head->next;\r\n }\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n ListNode random = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode copyRandomList(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n ListNode[] arr = new ListNode[n];\r\n ListNode prev = null;\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = new ListNode(0);\r\n if (prev != null)\r\n prev.next = arr[i];\r\n prev = arr[i];\r\n }\r\n\r\n for (int i = 0; i < n; i++) {\r\n int val = scn.nextInt();\r\n int idx = scn.nextInt();\r\n\r\n arr[i].val = val;\r\n if(idx != -1) arr[i].random = arr[idx];\r\n }\r\n\r\n ListNode head = copyRandomList(arr[0]);\r\n while (head != null) {\r\n System.out.print(\"(\" + head.val + \", \" + (head.random != null ? head.random.val : -1) + \") \");\r\n head = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7 -1\r\n15 0\r\n18 4\r\n10 2\r\n5 0","sampleOutput":"(7, -1) (15, 7) (18, 5) (10, 18) (5, 7) ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"747f2850-e8c6-421d-a657-f37a19750523","name":"Copy Linkedlist With Random Pointers","slug":"copy-linkedlist-with-random-pointers","type":1}],"next":{"id":"da5ebaf3-96f0-4b9c-a552-b72fa3c9358e","name":"Copy Linkedlist With Random Pointers MCQ","type":0,"slug":"copy-linkedlist-with-random-pointers-mcq"},"prev":{"id":"c0cb1b8c-b117-42a0-ab54-45ce3bbc0334","name":"Reverse In Range","type":3,"slug":"reverse-in-range"}}}`

Editor

# Copy Linkedlist With Random Pointers

easy

1. Given a singly linklist with an additional random pointer which could point to any node in the list or NULL. 2. Return a deep copy of the list. 3. For More Details watch Video.

0 <= N <= 10^6

## Format

### Input

input is handle for you

### Output

output is handle for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 7 -1 15 0 18 4 10 2 5 0```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}(7, -1) (15, 7) (18, 5) (10, 18) (5, 7) `

Discussions

Show Discussion

Related Resources