GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/linkedlist/copy_random_pointer.py at master · vspriya05/algorithms Expand file tree/ Copy pathcopy_random_pointer.pyMore file actions 36 lines (33 loc) · 928 Bytes / Copy pathcopy_random_pointer.pyFile metadata and controls36 lines (33 loc) · 928 Bytes 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 26 27 28 29 30 31 32 33 34 35 36 """ 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. """ class Solution: # @param head, a RandomListNode # @return a RandomListNode def copyRandomList(self, head): dic = dict() m = n = head while m: dic[m] = RandomListNode(m.label) m = m.next while n: dic[n].next = dic.get(n.next) dic[n].random = dic.get(n.random) n = n.next return dic.get(head) #O(n) class Solution: # @param head, a RandomListNode # @return a RandomListNode def copyRandomList(self, head): copy = collections.defaultdict(lambda: RandomListNode(0)) copy[None] = None node = head while node: copy[node].label = node.label copy[node].next = copy[node.next] copy[node].random = copy[node.random] node = node.next return copy[head] |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |