链表面试题复制链表

题目:题目链接

题目文字描述:

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

分析: 

在这里介绍两种解法:

1.指利用链表

2.利用Map集合

 

1>利用链表

public class Practice2 {

    static class Node{
        int val;
        Node random;
        Node next;
        Node(){

        }
        Node(int val){
            this.val = val;
        }
    }
    public static void print(Node head){
        if(head == null){
            return;
        }
        Node cur;
        for(cur = head;cur != null; cur = cur.next){
            System.out.printf("Node(%d)--->",cur.val);
        }
        System.out.printf("null");
    }
    public static Node test(){
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = null;
        n1.random = null;
        n2.random = n1;
        n3.random = n3;
        n4.random = n3;
        return n1;
    }
    //复制链表
    public static Node copyRandomList(Node head) {
        Node cur = head;
        while(cur != null){
            Node node = new Node(cur.val);
            node.next = cur.next;
            cur.next = node;
            cur = cur.next.next;
        }
        //复制random
        cur = head;
        while(cur != null){
            if(cur.random != null) {
                cur.next.random = cur.random.next;
            }else{
                cur.next.random = null;
            }
            cur = cur.next.next;
        }
        //将两个链表分开
        Node result = head.next;
        cur = head;
        Node last = result;
        while(cur != null) {
            cur.next = last.next;
            if(cur.next != null) {
                last.next = cur.next.next;
            }else{
                last.next = null;
            }
            cur = cur.next;
            last = last.next;
        }
        return result;
    }

    public static void main(String[] args) {
        print(copyRandomList(test()));

    }
}

 

2>利用Map集合

Map定义为Map,因为要将节点放入集合中

import java.util.*;
public class Practice3 {
    //定义节点
    static class Node{
        int val;
        Node random;
        Node next;
        Node(){

        }
        Node(int val){
            this.val = val;
        }
    }
    //打印链表的方法
    public static void print(Node head){
        if(head == null){
            return;
        }
        Node cur;
        for(cur = head;cur != null; cur = cur.next){
            System.out.printf("Node(%d)--->",cur.val);
        }
        System.out.printf("null");
    }
    //创建一个链表
    public static Node test(){
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = null;
        n1.random = null;
        n2.random = n1;
        n3.random = n3;
        n4.random = n3;
        return n1;
    }
    //复制链表
    public static Node copyRandomList(Node head) {
        Node cur = head;
        Node result = null;
        Node last = null;
        Map map = new HashMap<>();
        while(cur != null){
            Node node  = new Node(cur.val);
            map.put(cur,node);
            if(result == null){
                result = node;
            }else{
                last.next = node;
            }
            last = node;
            cur = cur.next;
        }
        //复制random指针
        cur = head;
        last = result;
        while(cur != null){
            last.random = map.get(cur.random);
            cur = cur.next;
            last = last.next;
        }
        return result;
    }

    public static void main(String[] args) {
        print(copyRandomList(test()));
    }
}

链表面试题复制链表_第1张图片

你可能感兴趣的:(leetcode刷题)