牛客JZ35——复杂链表的复制(JAVA)

题目描述:
https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba

思路:因为不可以用原来链表中的结点,所以链表里面的每一个节点我们都要重新新建一遍
方法一:使用hash表,首先遍历链表,把节点本身和新建的对应节点都放入map中,然后再一次遍历链表,并从hash表中取出对应的值
res链表为返回的结果
p为进行复制的链表
pHead作为放入map的数据
head作为第二次遍历链表的头结点

方法二:双指针法
把每一个节点复制并且插入原来的链表中,即原来1-2-3变成1-1(拷贝)-2-2(拷贝)-3-3(拷贝)
然后再次遍历链表把对应的random指针也进行拷贝
最后再次遍历链表将链表拆成两个链表即可

1.hashmap

import java.util.HashMap;
 /*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
// 方法一 hashmap
// 如果只保存random到map表里面会报错
// wrong,cannot used original node of list
public class Solution {
    public RandomListNode Clone(RandomListNode pHead) {
        if(pHead==null) return pHead;
        
        HashMap<RandomListNode,RandomListNode> map=new HashMap<>();
        
        RandomListNode res=new RandomListNode(pHead.label);
        
        RandomListNode p=res;
        
        RandomListNode head=pHead;
        
        while(pHead!=null){
            map.put(pHead,new RandomListNode(pHead.label));
            pHead=pHead.next;
        }
        
        while(head!=null){
            p.next=map.get(head.next);
            p.random=map.get(head.random);
            p=p.next;
            head=head.next;
        }
        return res;

    }
}




2.双指针

import java.util.HashMap;
 /*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
public class Solution {
    public RandomListNode Clone(RandomListNode pHead) {
        if(pHead==null)
        return pHead;
        RandomListNode head=pHead;
        RandomListNode h=pHead;
        // 复制
        while(pHead!=null){
            RandomListNode clone=new RandomListNode(pHead.label);
            clone.next=pHead.next;
            pHead.next=clone;
            pHead=clone.next;
        }
        // 复制random
        RandomListNode clone=head.next;
        while(head!=null){
            if(head.random==null){
                clone.random=null;
            }
            else{
                clone.random=head.random.next;
            }
            head=head.next.next;
            if(clone.next!=null){ //要检查,因为拷贝的下一个可能就是null,而null没有next,如果不检查就会报错
                clone=clone.next.next;
            }
        }
        //拆分
        clone=h.next;
        RandomListNode res=clone;
        while(h!=null){
            h.next=h.next.next;
            h=h.next;
            if(clone.next!=null){
                clone.next=clone.next.next;
            }
            clone=clone.next;
        }
        return res;
    }
}



### 关于Java模板链表的实现 在Java中,可以通过泛型来创建一个支持多种数据类型的链表结构。这种链表被称为“模板链表”,因为它能够接受任何类型的数据作为节点的内容。下面是一个简单的单向链表(Singly Linked List)的实现方式。 #### 单向链表的节点定义 首先需要定义链表中的节点类 `Node`,它包含两部分:存储的数据和指向下一个节点的引用。 ```java class Node<T> { T data; Node<T> next; public Node(T data) { this.data = data; this.next = null; } } ``` #### 链表的主要功能实现 接下来定义链表的核心操作,比如添加节点、删除节点以及遍历链表等。 ```java public class LinkedListTemplate<T> { private Node<T> head; // 添加元素到链表头部 public void addFirst(T value) { Node<T> newNode = new Node<>(value); newNode.next = head; head = newNode; } // 删除第一个匹配的元素 public boolean remove(T key) { if (head == null) return false; if (head.data.equals(key)) { head = head.next; return true; } Node<T> current = head; while (current.next != null && !current.next.data.equals(key)) { current = current.next; } if (current.next != null) { current.next = current.next.next; return true; } return false; } // 打印链表内容 public void display() { Node<T> temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } } ``` 上述代码展示了如何使用泛型 `<T>` 来构建一个通用的链表结构[^1]。这样做的好处是可以让同一个链表处理不同类型的对象而不必修改底层逻辑。 #### 测试代码示例 为了验证上面的链表实现是否正确工作,可以编写如下测试程序: ```java public class TestLinkedList { public static void main(String[] args) { LinkedListTemplate<Integer> list = new LinkedListTemplate<>(); list.addFirst(10); list.addFirst(20); list.addFirst(30); list.display(); // 输出: 30 -> 20 -> 10 -> null list.remove(20); list.display(); // 输出: 30 -> 10 -> null } } ``` 这段代码演示了如何向链表中插入整数并打印出来,同时也显示了移除特定值后的效果[^2]。 ### 总结 通过以上介绍可以看出,在Java里利用泛型机制很容易就能开发出具有高度灵活性的容器类——即所谓的“模板链表”。这样的设计不仅提高了代码复用率还增强了其适应能力[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值