Leetcode: Copy list with random pointer

Source: Internet
Author: User
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.

Difficulty: 77. The first idea is to copy a normal linked list first. during replication, the copied node is made into a hashmap, with the old node as the key and the new node as the value. The purpose is to connect the random pointer of the node according to the hash table during the second scan. A total of two scans are required, so the time complexity is O (2 * n) = O (n ). A hash table is required for node ing in the space, so the space complexity is O (n ).

 1 /** 2  * Definition for singly-linked list with a random pointer. 3  * class RandomListNode { 4  *     int label; 5  *     RandomListNode next, random; 6  *     RandomListNode(int x) { this.label = x; } 7  * }; 8  */ 9 public class Solution {10     public RandomListNode copyRandomList(RandomListNode head) {11         if (head == null) return null;12         RandomListNode dummy = new RandomListNode(0);13         RandomListNode scan = dummy;14         RandomListNode cur = head;15         HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();16         while (cur != null) {17             scan.next = new RandomListNode(cur.label);18             map.put(cur, scan.next);19             cur = cur.next;20             scan = scan.next;21         }22         cur = head;23         scan = dummy;24         while (cur != null) {25             scan.next.random = map.get(cur.random);26             cur = cur.next;27             scan = scan.next;28         }29         return dummy.next;30     }31 }

After all, this method requires a hash table to store and occupy extra space, which is not optimal. Therefore, consider whether additional space is not required. By referring to the idea on the Internet, we found that to avoid extra space, we can only store nodes by using the original data structure of the linked list. The basic idea is to scan the linked list three times, copy each node for the first scan, and insert the copied new node into the next of the original node, that is to say, let the linked list become a repeated linked list, that is, the old and new replicas. In the second scan, we assigned the random pointer of the old node to the random pointer of the new node, because all the new nodes are next to the old node, the assignment is simple, that is, the node. next. random = node. random. next, where node. next is the new node, because we connect the new node to the old node after the first scan. Now we have connected all the random pointers of the nodes. We split the Linked List into two for the last scan. The first one is to restore the original linked list, and the second is the replication linked list we requested. Because now the linked list is a new replacement, you only need to connect every two nodes and split the linked list. This method performs a total of three linear scans, so the time complexity is O (n ). Here, there is no extra space, so the space complexity is O (1 ). Compared with the above method, here we do a linear scan, but there is no extra space, or compare the value.

Difficulty in this practice: 95

Leetcode: Copy list with random pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.