剑指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;
    }
    

全部评论

相关推荐

今天 14:00
门头沟学院 运营
点赞 评论 收藏
分享
屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
lllllkin:感觉可以精简到一页简历,有些排版感觉不是必须的。 时间线越早的,你自己越熟悉的放前面。描述可以更精简些,一些问题解决感觉可以不用写具体技术栈,卖个关子,等面试官问。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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