[leetcode]138. Copy List with Random Pointer@Java解题报告
来源:互联网 发布:个人房源采集软件 编辑:程序博客网 时间:2024/06/05 16:35
https://leetcode.com/problems/copy-list-with-random-pointer/description/
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.
package go.jacob.day804;import java.util.HashMap;import java.util.Map;/** * 138. Copy List with Random Pointer 题意是:返回一个链表的深拷贝 * * @author Jacob * */public class Demo2 {/* * 推荐解答:使用map,每一个原链表节点,在map中对应新链表的节点 */public RandomListNode copyRandomList(RandomListNode head) {if (head == null)return null;Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();RandomListNode node = head;while (node != null) {map.put(node, new RandomListNode(node.label));node = node.next;}node = head;while (node != null) {map.get(node).next = map.get(node.next);map.get(node).random = map.get(node.random);node = node.next;}return map.get(head);}/* * 剑指offer中的解答,会改变原链表的结构。 需要额外操作来恢复原链表的结构 * 链表的题目画图比较清晰 */public RandomListNode copyRandomList_1(RandomListNode head) {if (head == null)return null;RandomListNode node = head;// 在原链表每个节点后插入一个复制的节点while (node != null) {RandomListNode newNode = new RandomListNode(node.label);RandomListNode temp = node.next;node.next = newNode;newNode.next = temp;node = temp;}node = head;// 复制随机节点while (node != null) {RandomListNode tempRandom = node.random;if (tempRandom != null) {node.next.random = tempRandom.next;} elsenode.next.random = null;node = node.next.next;}//恢复原链表和生成新链表RandomListNode newHead = head.next;RandomListNode newNode = head.next;node = head;while (newNode != null) {node.next=newNode.next;node = newNode.next;if (node != null){newNode.next = node.next;}elsenewNode.next = null;newNode = newNode.next;}return newHead;}private class RandomListNode {int label;RandomListNode next, random;RandomListNode(int x) {this.label = x;}};}
阅读全文
0 0
- [leetcode]138. Copy List with Random Pointer@Java解题报告
- [leetcode] 138. Copy List with Random Pointer 解题报告
- [Leetcode] 138. Copy List with Random Pointer 解题报告
- LeetCode — Copy List with Random Pointer 解题报告
- leetCode解题报告之Copy List with Random Pointer
- 【LeetCode】Copy List with Random Pointer 解题报告
- LeetCode 解题报告 Copy List With Random Pointer
- Copy List with Random Pointer解题报告
- Copy List with Random Pointer 解题报告
- [Leetcode] Copy List with Random Pointer (Java)
- [leetcode] Copy List with Random Pointer, Java
- Copy List with random pointer--Leetcode(Java)
- Copy List with Random Pointer leetcode java
- Copy List with Random Pointer的解题报告
- LeetCode 138. Copy List with Random Pointer
- [Leetcode] 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [leetcode] 138. Copy List with Random Pointer
- synchronized
- 总师组的工作模式和方法
- 6.3—排序—Merge k Sorted Lists
- 宏函数与普通函数的比较
- 自定义类型在Set容器里
- [leetcode]138. Copy List with Random Pointer@Java解题报告
- 6.4—排序—Insertion Sort List
- 安卓开发由一个APP拉起另一个APP的方法总结
- webrtc 服务器搭建
- 6.5—排序—Sort List
- 微信开发2
- Kali linux 2016.2(Rolling)里安装OpenVAS
- 6.6—排序—First Missing Positive
- 大学科研岗老师的工作与职责