法1:哈希表,O(n)+O(n)
原Node => 新Node这种方式真是简洁大方!
Python
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head:
return None
copy_map = dict()
cur = head
while cur:
copy_map[cur] = Node(cur.val)
cur = cur.next
cur = head
while cur:
copy_map[cur].next = None if not cur.next else copy_map[cur.next]
copy_map[cur].random = None if not cur.random else copy_map[cur.random]
cur = cur.next
return copy_map[head]
Java
class Solution {
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
Node cur = head;
Map<Node, Node> map = new HashMap();
while (cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
while (cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
return map.get(head);
}
}
法2:复制+拆分链表
需要缕清楚指向关系。
Python
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if head == None:
return head
cur = head
while cur != None:
next_node = cur.next
new_node = Node(cur.val, None, None)
cur.next = new_node
new_node.next = next_node
cur = next_node
cur = head
while cur != None:
cur.next.random = None if cur.random == None else cur.random.next
cur = cur.next.next
cur = head.next
while cur != None:
next_node = cur.next
cur.next = None if next_node == None else next_node.next
cur = cur.next
return head.next
Java
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
// 1.复制
Node cur = head;
while (cur != null) {
Node copyCur = new Node(cur.val);
copyCur.next = cur.next;
cur.next = copyCur;
cur = copyCur.next;
}
// 2.设置random
cur = head;
while (cur != null) {
if (cur.random != null) {
cur.next.random = cur.random.next;
}
cur = cur.next.next;
}
// 3.分割
cur = head.next;
Node pre = head, copyHead = head.next;
while (cur.next != null) {
pre.next = pre.next.next;
cur.next = cur.next.next;
pre = pre.next;
cur = cur.next;
}
pre.next = null;
return copyHead;
}
}
Solution1:
小套路记住就行!!!
根据书上复杂度为
O
(
n
)
O(n)
O(n)的算法写的,此题加深了对于链表指针的理解与应用!
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(pHead == NULL)
return NULL;
CloneNodes(pHead);
ConnectRan(pHead);
struct RandomListNode* pHead_clone = Get(pHead);
return pHead_clone;
}
void CloneNodes(RandomListNode* pHead){
struct RandomListNode* pNode = pHead;
while(pNode != NULL){
struct RandomListNode* pClone = new RandomListNode(pNode->label);
pClone->next = pNode->next;
pNode->next = pClone;
pNode = pClone->next;
}
return;
}
void ConnectRan(RandomListNode* pHead){
struct RandomListNode* pNode = pHead;
while(pNode != NULL){
struct RandomListNode* pClone = pNode->next;
if(pNode->random != NULL){
pClone->random = pNode->random->next;
}
pNode = pClone->next;
}
return;
}
struct RandomListNode* Get(RandomListNode* pHead){
struct RandomListNode* pNode1 = pHead, *pHead_Clone = pHead->next;
struct RandomListNode* pNode2 = pHead_Clone;
while(pNode1 != NULL){
pNode1->next = pNode2->next;
pNode1 = pNode1->next;
if(pNode1 == NULL)
break;
pNode2->next = pNode1->next;
pNode2 = pNode2->next;
}
return pHead_Clone;
}
};