{"id":22513943,"url":"https://github.com/emahtab/copy-list-with-random-pointer","last_synced_at":"2026-01-06T08:44:32.318Z","repository":{"id":79525381,"uuid":"240634851","full_name":"eMahtab/copy-list-with-random-pointer","owner":"eMahtab","description":"Copy a Linked List with Random Pointer","archived":false,"fork":false,"pushed_at":"2021-02-21T02:09:03.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T03:26:11.046Z","etag":null,"topics":["clone","leetcode","linked-list","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-15T02:51:00.000Z","updated_at":"2021-02-21T02:09:05.000Z","dependencies_parsed_at":"2023-05-22T18:15:44.796Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/copy-list-with-random-pointer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcopy-list-with-random-pointer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcopy-list-with-random-pointer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcopy-list-with-random-pointer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcopy-list-with-random-pointer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/copy-list-with-random-pointer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952239,"owners_count":20699452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clone","leetcode","linked-list","problem-solving"],"created_at":"2024-12-07T03:15:13.371Z","updated_at":"2026-01-06T08:44:32.287Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Copy a Linked List with Random Pointer\n## https://leetcode.com/problems/copy-list-with-random-pointer\n\nA linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.\n\nReturn a deep copy of the list.\n\nThe 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:\n\nval: an integer representing Node.val\nrandom_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.\n\n## From the past :\nWell, we already know how to clone a linked list. \nJust iterate through the linked list, create new node and connect the previous node with the current node, and update the pointers.\n\n```java\npublic ListNode clone(ListNode head) {\n     ListNode prev = null, copyHead = null, ptr = head;\n     while(ptr != null) {\n\tListNode node = new ListNode(ptr.val);\n\tif(prev == null) \n\t   copyHead = node;\n\tif(prev != null) {\n\t   prev.next = node;\n\t}\n\tprev = node;\n\tptr = ptr.next;\n     }\n   return copyHead;\n}\n```\nBut wait, this problem have an extra level of complexity. \n\nIn this list each node have a random pointer, which may point to a node, which may be forward in the list.\n\nSo we better first create all the nodes (in the first iteration) and in the second iteration just connect the next and random pointer correctly.\n\n\n## Implementation 1 : O(n)\n\n```java\n/*\n// Definition for a Node.\nclass Node {\n    int val;\n    Node next;\n    Node random;\n\n    public Node(int val) {\n        this.val = val;\n        this.next = null;\n        this.random = null;\n    }\n}\n*/\nclass Solution {\n    public Node copyRandomList(Node head) {\n        if (head == null)\n            return null;\n        Map\u003cNode, Node\u003e cloneMap = new HashMap\u003c\u003e();\n        Node curr = head;\n        while (curr != null) {\n          cloneMap.put(curr, new Node(curr.val));\n          curr = curr.next;\n        }\n        \n        curr = head;\n        while (curr != null) {\n            cloneMap.get(curr).next = cloneMap.get(curr.next);\n            cloneMap.get(curr).random = cloneMap.get(curr.random);\n            curr = curr.next;\n        }\n        return cloneMap.get(head);\n    }\n}\n\n```\n\n## Implementation 2 : O(1)\n\n```java\n/*\n// Definition for a Node.\nclass Node {\n    int val;\n    Node next;\n    Node random;\n\n    public Node(int val) {\n        this.val = val;\n        this.next = null;\n        this.random = null;\n    }\n}\n*/\nclass Solution {\n    public Node copyRandomList(Node head) {\n      if (head == null)\n            return null;\n      Node curr = head;\n      Node next = null;\n        \n      while (curr != null) {\n       Node copy = new Node(curr.val);     \n       next = curr.next;\n       curr.next = copy;\n       copy.next = next;\n       curr = next;\n      }\n        \n      curr = head;\n      while (curr != null) {\n          if (curr.random != null) { \n            curr.next.random = curr.random.next;\n          }\n          curr = curr.next.next;    \n      }\n        \n      curr = head;\n      Node cloneListHead = curr.next; \n        \n      while(curr != null){\n          next = curr.next.next;\n          Node cloneNode = curr.next;\n          cloneNode.next = (next != null) ? next.next : null;\n          curr.next = next;\n          curr = next;\n      }  \n      return cloneListHead;   \n   }\n}\n```\n\n💥 ❗️  Caution : NullPointerException\n\n# References :\n1. https://www.youtube.com/watch?v=OvpKeraoxW0\n2. https://github.com/bephrem1/backtobackswe/tree/master/Linked%20Lists/CloneLinkedListWithRandomPointers\n3. https://leetcode.com/articles/copy-list-with-random-pointer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcopy-list-with-random-pointer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcopy-list-with-random-pointer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcopy-list-with-random-pointer/lists"}