AcWing 48. 复杂链表的复刻
原题链接
中等
作者:
nullwh
,
2020-09-22 09:35:40
,
所有人可见
,
阅读 444
class Solution {
public:
ListNode *copyRandomList(ListNode *head) {
for (auto p = head; p; ) {
auto np = new ListNode(p->val);
auto next = p->next;
p->next = np;
np->next = next;
p = next;
}
for (auto p = head; p; p = p->next->next) {
if (p->random)
p->next->random = p->random->next;
}
auto dummy = new ListNode(-1);
auto cur = dummy;
for (auto p = head; p; p = p->next) {
cur->next = p->next;
cur = cur->next;
p->next = p->next->next;
}
return dummy->next;
}
};