[LeetCode] 138. Copy List with Random Pointer_Medium tag: Linked List

这个题目有两种做法,

note: 注意head.random有可能是None的情况

1. S: O(n)    create一个dictionary,然后先建dictionary和copy list以及next指针。然后再一遍,去建random指针。

2: S: O(1)    利用如下图所示的结构,然后再将copy node和原来的node分开即可。

Old List: A --> B --> C --> D
InterWeaved List: A --> A‘ --> B --> B‘ --> C --> C‘ --> D --> D‘

1. code S:O(n)
class Node:
    def __init__(self, x, next, random):
        self.val = x
        self.next = next
        self.random = random

class Solution:
    def deepCopyList(self, head):
        dummy, listMap = Node(0, None, None), dict()
        head1, head2 = head, dummy
        # copy the list and next pointers
        while head1:
            head2.next = Node(head1.val, None, None)
            ListMap[head1] = head2.next
            head1 = head1.next
            head2 = head2.next
        # copy the random pointers
        head1, head2 = head, dummy.next
        while head1:
            if head1.random: # head1.random can be None
                head2.random = listMap[head1.random]
            head1 = head1.next
            head2 = head2.next
        return dummy.next

2. code     S: O(1)

def deepCopyList(self, head):
    dummy = Node(0, None, None)
    dummy.next = head
    # make copy nodes
    while head:
        temp = head.next
        head.next = Node(head.val, None, None)
        head.next.next = temp
        head = temp
    # create random pointers
    head = dummy.next
    while head:
        if head.random:
            head.next.random = head.random.next
        head = head.next.next
    # make copy list and recover the original list
    dummy2 = Node(0, None, None)
    head1, head2 = dummy.next, dummy2
    while head1:
        head2.next = head1.next
        head1.next= head1.next.next
        head1 = head1.next
        head2 = head2.next
    return dummy2.next

原文地址:https://www.cnblogs.com/Johnsonxiong/p/10807771.html

时间: 2024-08-01 07:20:22

[LeetCode] 138. Copy List with Random Pointer_Medium tag: Linked List的相关文章

Java for LeetCode 138 Copy List with Random Pointer

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. 解题思路: 我们在Java for LeetCode 133 Clone Graph题中做过图的复制,本题和图的复制十分类似,JAVA实现如下: public Random

Leetcode 138. Copy List with random pointer

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. 思路:deep copy的意思就是克隆.扫两遍原来的list,第一遍copy node和next.然后再扫第二遍,这是如果pointer.random非空,我们就可以在co

leetcode 138. Copy List with Random Pointer ----- java

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. 和第133题差不多,都是图的复制,区别在于这道题的label有可能是相同的,所以导致了map的key有可能相同,所以需要处理. 两种方法差不多.第二种更简洁. 1.在复制n

[leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, 每一个节点都是新建的,而不是指向就节点 这个题的难点在于:随机节点. 随机节点有可能指向后边还没有建立的节点,这就没法指. 方法一:一一对应地记录每个新旧节点的映射关系,在常规节点建立后,就去查哈希表,找到对应 新节点的旧节点的random,就是新节点的random */ if (head==nu

133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

138. Copy List with Random Pointer(js)

138. Copy List with Random Pointer 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. Example 1: Input: {"$id":"1","n

【LeetCode】Copy List with Random Pointer

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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

[LeetCode][JavaScript]Copy List with Random Pointer

Copy List with Random Pointer 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. https://leetcode.com/problems/copy-list-with-random-poin

leetcode -day8 Copy List with Random Pointer & Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer 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. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点