leetcode--011 copy list with random pointer

 

 

 1 package leetcode;
 2 
 3 class RandomListNode {
 4     int label;
 5     RandomListNode next, random;
 6 
 7     public RandomListNode(int x) {
 8         this.label = x;
 9     }
10 }
11 
12 public class CopyListRandom {
13     public RandomListNode copyRandomList(RandomListNode head) {
14         if(head==null)return head;
15         RandomListNode po= head.next;
16         RandomListNode p=head;
17         while(p!=null){
18             RandomListNode newnode=new RandomListNode(p.label);
19             po=p.next;
20             newnode.next=p.next;
21             p.next=newnode;
22 
23             p=po;            
24         }
25         p=head;
26         while(p!=null){
27             if(p.random!=null){
28             p.next.random=p.random.next;
29         }
30             p=p.next.next;
31         }
32         p=head;
33         RandomListNode h=head.next;
34         RandomListNode q=h;
35         while(q!=null){
36             p.next=q.next;
37             p=q;
38             q=q.next;            
39         }
40         return h;
41     }
42 }

 

转载于:https://www.cnblogs.com/thehappyyouth/p/3880647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值