NC260. 复杂链表的复制
描述
示例1
输入:
{1,2,3,4,5,3,5,#,2,#}
输出:
{1,2,3,4,5,3,5,#,2,#}
Javascript 解法, 执行用时: 1ms, 内存消耗: 380KB, 提交时间: 2021-11-13
function RandomListNode(x){ this.label = x; this.next = null; this.random = null; } function Clone(pHead) { // write code here //思路:复制节点,赋值random,拆分节点 if(pHead===null){return null} //复制节点 let temp = pHead while(temp){ let copyNode =new RandomListNode(temp.label) copyNode.next = temp.next temp.next = copyNode temp = temp.next.next } //赋值random temp = pHead while(temp){ if(temp.random){ temp.next.random = temp.random.next } else{temp.next.random = null} temp = temp.next.next } //拆分节点 let root = pHead.next let r = root temp = pHead.next while(temp){ if(temp.next===null){break} temp = temp.next.next r.next = temp r = r.next } //返回新链表头节点 return root } module.exports = { Clone : Clone };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-03-05
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { if (!pHead) return NULL; RandomListNode *head = pHead; while (head) { RandomListNode *clone= new RandomListNode(head->label); //clone->label = head->label; clone->next = head->next; head->next = clone; head = clone->next; } RandomListNode *head1 = pHead; RandomListNode *clone1; while (head1) { clone1 = head1->next; if (head1->random != nullptr) clone1->random = head1->random->next; head1 = clone1->next; } RandomListNode *head2 = pHead; RandomListNode *clone2; RandomListNode *cloned = pHead->next; while (head2) { clone2 = head2->next; head2->next = clone2->next; head2 = head2->next; if(head2) clone2->next = head2->next; } return cloned; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-03-03
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public: struct INFO{ RandomListNode *ori_now; RandomListNode *res_now; }; RandomListNode* Clone(RandomListNode* pHead) { if( !pHead ) return NULL; INFO info_arr[88]; int idx = 1; RandomListNode *res = new RandomListNode( pHead->label ), *Last = res; info_arr[0].ori_now = pHead; info_arr[0].res_now = res; for(pHead=pHead->next; pHead; pHead=pHead->next){ Last->next = new RandomListNode( pHead->label ); Last = Last->next; info_arr[idx].ori_now = pHead; info_arr[ idx++ ].res_now = Last; } int i, j; for(i=0; i<idx; i++){ Last = info_arr[i].ori_now->random; for(j=0; j<idx; j++) if(info_arr[j].ori_now == Last){ info_arr[i].res_now->random = info_arr[j].res_now; break; } } return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-06-07
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { if(!pHead) return nullptr; RandomListNode* pHead1 = pHead; while(pHead1 != nullptr){ RandomListNode* pclone = new RandomListNode(pHead1 -> label); //pclone -> label = pHead1 -> label; pclone -> next = pHead1 -> next; pHead1 -> next = pclone; //pclone -> random = pHead1 -> random; pHead1 = pclone -> next; } RandomListNode *head1 = pHead; RandomListNode *clone1; while (head1) { clone1 = head1->next; if (head1->random != nullptr) clone1->random = head1->random->next; head1 = clone1->next; } RandomListNode *head2 = pHead; RandomListNode *clone2; RandomListNode *cloned = pHead->next; while (head2) { clone2 = head2->next; head2->next = clone2->next; head2 = head2->next; if(head2) clone2->next = head2->next; } return cloned; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-05-24
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { if(pHead==NULL) return NULL; map<RandomListNode*,RandomListNode*> record; RandomListNode* now=pHead; while(now!=NULL){ record.insert(pair<RandomListNode*,RandomListNode*>(now,new RandomListNode(now->label))); now = now->next; } now=pHead; RandomListNode* ans=new RandomListNode(pHead->label); RandomListNode* p; p = ans; while(now!=NULL){ p->next = record[now->next]; p->random = record[now->random]; now = now->next; p = p->next; } return ans; } };