Copy List with Random Pointer
来源:互联网 发布:手机淘宝怎样朋友代付 编辑:程序博客网 时间:2024/06/15 17:41
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.
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * }; */public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if(head==null) return null; RandomListNode newHead=new RandomListNode(head.label); Map<RandomListNode,RandomListNode> map=new HashMap<RandomListNode,RandomListNode>(); map.put(head,newHead); RandomListNode node=head.next; RandomListNode p=newHead; while(node!=null){ RandomListNode copy=new RandomListNode(node.label); p.next=copy; p=p.next; map.put(node,p); node=node.next; } node=head; while(node!=null){ map.get(node).random=map.get(node.random); node=node.next; } return newHead; }}思路:1、复制原链表上的每个节点,把<node,newnode>的陪对信息放入HashMap中;2、根据哈希表复制Random节点
0 0
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Python爬虫入门教程
- POJ 1002 487-3279 MAP||Sort
- char * a, char ** a, char * a[], char a[][], char * a[][], char ** a[][], char * a [][][], and so on
- 适合国人使用的源代码托管服务空间(本土化的github)
- hdu 4902 线段树双标记类型题
- Copy List with Random Pointer
- Android SDK离线安装方法详解(加速安装)
- 使用autotools生成Makefile
- 黑马程序员 java学习笔记——javaBean
- 求逆元方法总结
- linux基础命令
- EditText属性简介
- DM6437外设
- PAT 1082. Read Number in Chinese (25)