Question: https://oj.leetcode.com/problems/copy-list-with-random-pointer/
Question Name: Copy List with Random Pointer
Quite similar with the Clone Graph by LeetCode.
Solution to Copy List with Random Pointer by LeetCode
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # Definition for singly-linked list with a random pointer. # class RandomListNode: # def __init__(self, x): # self.label = x # self.next = None # self.random = None class Solution: # @param head, a RandomListNode # @return a RandomListNode def copyRandomList(self, head): # Store the mapping between old nodes and new nodes mapOldToNew ={None:None} # Clone all the old nodes with only labels. current = head while current != None: mapOldToNew[current] = RandomListNode(current.label) current = current.next # Assign the next and random of new cloned nodes. current = head while current != None: mapOldToNew[current].next = mapOldToNew[current.next] mapOldToNew[current].random = mapOldToNew[current.random] current = current.next # Return the cloned head return mapOldToNew[head] |
Happy Father’s Day!