LeetCode 138. Copy List with Random Pointer
来源:互联网 发布:python中的sleep 编辑:程序博客网 时间:2024/06/01 08:24
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.
这个可以有两种方式去做,一个是使用正常的方式,使用map进行存储,另一个是使用一个相当trick的方式进行处理,将改点复制然后放置到后面,最后拆分链表
java
/** * 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; } head = copyNode(head); head = copyEdge(head); return splitNode(head); } private RandomListNode copyNode(RandomListNode head) { if (head == null) { return null; } RandomListNode node = head; while (node != null) { RandomListNode temp = new RandomListNode(node.label); temp.next = node.next; node.next = temp; node = node.next.next; } return head; } private RandomListNode copyEdge(RandomListNode head) { if (head == null) { return null; } RandomListNode node = head; while (node != null) { if (node.random != null) { node.next.random = node.random.next; } node = node.next.next; } return head; } private RandomListNode splitNode(RandomListNode head) { RandomListNode val = head.next; while (head != null) { RandomListNode temp = head.next; head.next = temp.next; head = head.next; if (temp.next != null) { temp.next = temp.next.next; } } return val; }}python
# Definition for singly-linked list with a random pointer.# class RandomListNode(object):# def __init__(self, x):# self.label = x# self.next = None# self.random = Noneclass Solution(object): def copyRandomList(self, head): """ :type head: RandomListNode :rtype: RandomListNode """ if head is None: return head head = self.copyNode(head) head = self.copyEdge(head) return self.splitNode(head) def copyNode(self, head): if head is None: return head node = head while node is not None: temp = RandomListNode(node.label) temp.next = node.next node.next = temp node = node.next.next return head def copyEdge(self, head): if head is None: return head node = head while node is not None: if node.random is not None: node.next.random = node.random.next node = node.next.next return head def splitNode(self, head): if head is None: return head node = head.next while head is not None: temp = head.next head.next = temp.next head = head.next if temp.next is not None: temp.next = temp.next.next return node
阅读全文
0 0
- 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
- 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
- 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
- [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
- leetcode 138. Copy List with Random Pointer
- Leetcode 138. Copy List with Random Pointer
- phpStudy配置虚拟域名
- unity自带寻路Navmesh入门教程(一)
- 9.2 构造器与垃圾收集器
- 上班三年
- tf.control_dependencies()和tf.identity()
- LeetCode 138. Copy List with Random Pointer
- git merge冲突产生
- 代码安全部分总结
- Java| Javadoc生成Java帮助文档
- Vue学习日志:模板语法(3)
- elasticsearch实现远程索引复制
- integer-to-roman and roman-to-integer罗马数字转换
- Linux基础之九
- Magento适应Seaslog和Logstash要求的书写规范