momo's

Cracking LeetCode: 138. Copy List with Random Pointer

May
11

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.


Thought:

Associate the original list and the copy list side by side, and after copying the random pointer, split the original list and the copy list.

Use O(1) extra space and O(n) time complexity.

 

Reference:

https://discuss.leetcode.com/topic/7594/a-solution-with-constant-space-complexity-o-1-and-linear-time-complexity-o-n/2

 

Tech/技术 , , Comments Off on Cracking LeetCode: 138. Copy List with Random Pointer

Comments

Comments are closed.

Scroll Up