[LeetCode OJ] Copy List with Random Pointer extension, leetcodeoj
Address: https://oj.leetcode.com/problems/copy-list-with-random-pointer/
Deep replication of a loop linked list
Problem solving: After I got an AC, I found that I understood the question wrong. Then I referred to the following article,
If you want to see how to solve this problem, please move to the http://www.2cto.com/kf/201310/253477.html
Extended: If the node given by the question is not the head node of the linked list, but any node in the linked list, the linked list will be copied in depth even if the node from which the question is directed can traverse all nodes.
So what should we do?
Thought: in fact, the difference from the original question is only whether the node is the first node, so we can find the original node from the node.
The traversal process uses dfs and map to determine whether a node has been traversed. Now the only difficulty is how to find the header node. It is okay to use and check the set here.
The complexity of path compression and query set is close to O (1). Because the complexity of map is close to O (logN), the complexity of dfs is probably O (NlogN ).
Tip: When dfs arrives at a new node, map it to a unique integer cnt with map, and then initialize and query the fa [cnt].
// # Include "stdafx. h "# include <iostream> # include <string. h ># include <map >#include <set> using namespace std; int fa [10024]; // number of nodes inline int findfa (int x) {if (fa [x] = x) return x; while (x! = Fa [x]) {x = fa [x];} return x;} inline int merge (int x, int y) {int fa1 = findfa (x ), fa2 = findfa (y); if (fa1! = Fa2) {fa [fa2] = fa1;} return fa1;} struct RandomListNode {int label; RandomListNode * next, * random; RandomListNode (int x): label (x ), next (NULL), random (NULL) {}; class Solution {map <RandomListNode *, int> mp; map <RandomListNode *, int >:: iterator it; int cnt; public: RandomListNode * copyRandomList (RandomListNode * head) {if (! Head) return head;/*********** the comments section is the expanded solution ************* init (); dfs (head, 1); int faNum = findfa (1); for (it = mp. begin (); it! = Mp. end (); ++ it) {if (it-> second = faNum) {head = it-> first; break ;}} **************************************** * *****/return deepCopy (head );} void init () {memset (fa, 0, sizeof (fa); cnt = 0;} void dfs (RandomListNode * ptr, int faCnt) {if (ptr = NULL) return; it = mp. find (ptr); if (it! = Mp. end () return; mp [ptr] = ++ cnt; fa [cnt] = cnt; // initialize fa [cnt] = findfa (faCnt> 0? FaCnt: cnt); RandomListNode * next = ptr-> next; RandomListNode * random = ptr-> random; it = mp. find (next); if (it = mp. end () {dfs (next, faCnt> 0? FaCnt: cnt);} else {fa [mp [next] = fa [cnt];} it = mp. find (random); if (it = mp. end () {dfs (random, 0); // 0 indicates that the parent node cannot be determined} // RandomListNode * deepCopy (RandomListNode * head) {// duplicate copy the linked list RandomListNode * ptr1 = head, * ptr2 = NULL; while (ptr1! = NULL) {ptr2 = new RandomListNode (ptr1-> label); ptr2-> next = ptr1-> next; ptr1-> next = ptr2; ptr1 = ptr2-> next ;} // copy the random relation ptr1 = head, ptr2 = NULL; while (ptr1! = NULL) {ptr2 = ptr1-> next; if (ptr1-> random) ptr2-> random = ptr1-> random-> next; ptr1 = ptr2-> next ;} // split the original list into two RandomListNode * H = NULL, * ptr3; ptr1 = head, ptr2 = NULL; while (ptr1! = NULL) {ptr2 = ptr1-> next; if (H = NULL) {H = ptr2; ptr3 = H;} else {ptr3-> next = ptr2; ptr3 = ptr2;} ptr1-> next = ptr2-> next; ptr1 = ptr1-> next;} return H ;}};