LeetCode 138. Copy List with Random Pointer

分析

难度 中
来源 https://leetcode.com/problems/copy-list-with-random-pointer/
思路

  1. Iterate the original list and duplicate each node. The duplicate
    of each node follows its original immediately.
  2. Iterate the new list and assign the random pointer for each
    duplicated node.
  3. Restore the original list and extract the duplicated nodes.

题目

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.

Example 1:

Input:

{"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}

Explanation:
Node 1’s value is 1, both of its next and random pointer points to Node 2.
Node 2’s value is 2, its next pointer points to null and its random pointer points to itself.

Note:

  1. You must return the copy of the given head as a reference to the cloned list.

解答

Runtime: 0 ms, faster than 100.00% of Java online submissions for Copy List with Random Pointer.
Memory Usage: 36 MB, less than 99.69% of Java online submissions for Copy List with Random Pointer.

package LeetCode;

public class L138_CopyListWithRandomPointer {
    public Node copyRandomList(Node head) {
        if(head==null)
            return head;
        //复制每个节点
        Node index=head;
        while(index!=null){//复制的都是非空节点。
            Node next=index.next;
            index.next=new Node(index.val,next,null);
            index.next.next=next;//插入index节点的复制节点。
            index=next;//跳两步,到原来的下一个节点位置。
        }
        //设置random
        index=head;
        while(index!=null){
            if(index.random!=null)
                index.next.random = index.random.next;
            else
                index.next.random = null;
            index = index.next.next;//跳两步,到原来的下一个节点位置。
        }

        //拆分链表
        index=head;
        Node copyIndex,copyHead;
        copyIndex=copyHead=head.next;
        //while(copyIndex!=null&&copyIndex.next!=null)
        while(copyIndex.next!=null)//index.next.next节点为null时,大括号内第三步右侧null节点没有next,会产生空指针,所以判断条件不能使用index!=null
        {
            index.next=copyIndex.next;
            index=index.next;
            copyIndex.next=index.next;//null节点没有next
            copyIndex=copyIndex.next;
        }
        index.next=null;
        return copyHead;
    }
    public static void main(String[] args){
        L138_CopyListWithRandomPointer l138=new L138_CopyListWithRandomPointer();
       /* Node node1=new Node(1,null,null);
        Node node2=new Node(2,null,null);
        node2.random=node2;
        node1.next=node2;
        node1.random=node2;*/
        //Node node1=new Node(-1,null,null);

        //{"$id":"1","next":{"$id":"2","next":{"$id":"3","next":{"$id":"4","next":null,"random":null,"val":4},"random":null,"val":3},"random":{"$ref":"3"},"val":2},"random":{"$ref":"3"},"val":1}
        Node node1=new Node(1,null,null);
        Node node2=new Node(2,null,null);
        Node node3=new Node(3,null,null);
        Node node4=new Node(4,null,null);
        node1.next=node2;
        node1.random=node3;
        node2.next=node3;
        node2.random=node3;
        node3.next=node4;
        node3.random=null;
        node4.next=null;
        node4.random=null;

        Node index=node1;
        while(index!=null)
        {
            System.out.println(index+"\t"+ index.next+"\t"+index.random);
            index=index.next;
        }
        System.out.println();

        index=l138.copyRandomList(node1);
        while(index!=null)
        {
            System.out.println(index+"\t"+ index.next+"\t"+index.random);
            index=index.next;
        }
        System.out.println();

        index=node1;
        while(index!=null)
        {
            System.out.println(index+"\t"+ index.next+"\t"+index.random);
            index=index.next;
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值