您的位置:首页 > 运维架构

Algorithms—138.Copy List with Random Pointer

2015-07-20 10:06 507 查看
思路:很简单的一题,next当作主线递归赋值,random作为支线附上即可。

/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
RandomListNode answer=null;
if (head!=null) {
answer=new RandomListNode(head.label);
}else {
return null;
}
if (head.random!=null) {
answer.random=new RandomListNode(head.random.label);
}
answer.next=copyRandomList(head.next);
return answer;
}
}

耗时:356ms,中游
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: