剑指offer(25)复制含有随机指针节点的链表

class RandomListNode{
    int lebal;
    RandomListNode next;
    RandomListNode random;
    public RandomListNode(int lebal){
        this.lebal = lebal;
    }
}

public class NoTwentyfive {
    public static RandomListNode clone(RandomListNode pHead){
        if(pHead == null){
            return null;
        }
        
        RandomListNode cur = pHead;
        
        while(cur != null){
            RandomListNode next = cur.next;
            cur.next = new RandomListNode(cur.lebal);
            cur.next.next = next;
            cur = next;
        }
        
        cur = pHead;
        while(cur != null){
            RandomListNode pCloneHead = cur.next;
            RandomListNode next = cur.next.next;
            pCloneHead.random = cur.random == null ? null :cur.random.next;
            cur = next;
        }
        
        RandomListNode res = pHead.next;
        cur = pHead;
        while(cur != null){
            RandomListNode next = cur.next.next;
            RandomListNode pCloneHead = cur.next;
            cur.next = next;
            pCloneHead.next = next == null ? null : next.next;
            cur = next;
        }
        return res;
    }
    

全部评论

相关推荐

05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
04-29 00:12
小米_人力资源
牛客448863700号:也得看岗位呀,我还拿下美团呢,不说了送单了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务