【剑指offer】复杂链表的复制

瘦欲@ 提交于 2020-02-10 13:03:50

https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba?tpId=13&tqId=11178&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

 

class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;
    RandomListNode(int label) {
        this.label = label;
    }
}

public class Clone {
    public RandomListNode Clone(RandomListNode pHead)
    {
        RandomListNode p = pHead;
        RandomListNode t = pHead;
        while(p!=null){
            RandomListNode q = new RandomListNode(p.label);
            q.next = p.next;
            p.next = q;
            p = q.next;
        }
        while(t!=null){
            RandomListNode q = t.next;
            if(t.random != null) q.random = t.random.next;
            t = q.next;
        }
        RandomListNode s = new RandomListNode(0);
        RandomListNode s1 = s;
        while(pHead != null){
            RandomListNode q = pHead.next;
            pHead.next = q.next;
            q.next = s.next;
            s.next = q;
            s = s.next;
            pHead = pHead.next;
        }
        return s1.next;
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!