首页 > 代码库 > [LeetCode]138 Copy List with Random Pointer
[LeetCode]138 Copy List with Random Pointer
https://oj.leetcode.com/problems/copy-list-with-random-pointer/
http://blog.csdn.net/linhuanmars/article/details/22463599
/** * 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; Map<RandomListNode, RandomListNode> map = new HashMap<>(); RandomListNode old = head; RandomListNode precopied = null; while (old != null) { // Copy RandomListNode copied = new RandomListNode(old.label); if (precopied != null) precopied.next = copied; precopied = copied; map.put(old, copied); old = old.next; } RandomListNode newhead = map.get(head); while (head != null) { map.get(head).random = map.get(head.random); head = head.next; } return newhead; } }
[LeetCode]138 Copy List with Random Pointer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。