Copy list with random pointer

Source: Internet
Author: User

Title: https://oj.leetcode.com/problems/copy-list-with-random-pointer/

This topic provides a special single-chain table. Each node in the linked list has a pointer field: Random. Random, only to a node in the linked list. The question requires us to provide a deep copy of this linked list.

1. Shallow copy

During the copy, only the pointer or reference is copied, that is, only one copy of the data exists. Of course, when efficiency is achieved, the problem is that if the original data changes, the Copied object also changes, because there is only one copy of Data !!!

Note: in fact, it is out of efficiency considerations. In some cases, shallow copy can be used when multiple data copies are not required.

2. Deep copy

During the copy operation, not only the pointer or reference is copied, but the Copied object is not changed if the original data changes, because they have two different data copies.

3. Lazy copy

This is a combination of the above two policies. Also known as copy-on-write.

During initial replication, shallow copy with higher efficiency is used, and a Counter is used to record the number of objects in the current share data.

When you need to modify the data, use deep-copy according to counter,

That is, the time-consuming deep-copy is called only when deep-copy is required.

 

How do we solve this problem?

In fact, this question has appeared on the offoffer, and I have also implemented it. Today, the implementation is better than the previous implementation. Previous implementation: http://blog.csdn.net/huruzun/article/details/22292451

Idea: Step 1: first point to the end of each node in the original linked list and copy a new node. The length of the original linked list is doubled.

The random pointer points to the node next to the node pointed to by the original linked list node random pointer.

Step 2: Split the Linked List into two linked lists.

Note: separate two linked lists. Note that the status of the original linked list cannot be changed; otherwise, the AC cannot be implemented.

Public class solution {public randomlistnode copyrandomlist (randomlistnode head) {If (Head = NULL) {return NULL;} randomlistnode P = head; randomlistnode next = NULL; // complete copywhile (P! = NULL) {next = P. next; randomlistnode node = new randomlistnode (P. label); node. next = next; p. next = node; P = next;} p = head; randomlistnode q = P. next; // copy randomwhile (P! = NULL) {If (P. Random! = NULL) {q. Random = P. Random. Next;} p = P. Next. Next; // to move the two-step pointer of Q, you must first determine if (P! = NULL) {q = Q. next. next ;}} randomlistnode newhead = head. next; randomlistnode phead = head; randomlistnode qhead = newhead; // separate two linked lists. Note that the status of the original linked list cannot be changed. While (phead! = NULL) {phead. Next = qhead. Next; If (qhead. Next! = NULL) {qhead. Next = qhead. Next. Next;} phead = phead. Next; If (phead! = NULL) {qhead = qhead. Next;} return newhead ;}}


 

 

 

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.