-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Bug Report for https://neetcode.io/problems/copy-linked-list-with-random-pointer
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
"""
# 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: 'Optional[Node]') -> 'Optional[Node]':
curr = head
while curr:
temp = curr
curr = curr.next
newNode = Node(temp.val, curr, None)
temp.next = newNode
curr = head
copy = head.next
copyCurr = copy
while curr:
temp = curr
copyCurr.random = None if not temp.random else temp.random.next
curr.next = curr.next.next
curr = curr.next
copyCurr.next = None if not curr else curr.next
copyCurr = copyCurr.next
return copy
This is my solution but it's failing for
Input
head=[[3,null],[7,3],[4,0],[5,1]]
With error
Traceback (most recent call last):
File "/box/script.py", line 132, in process_input
output = linked_list_to_str(user_list) + " " + output
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
File "/box/script.py", line 110, in linked_list_to_str
res += "[" + str(head.val) + "," + (str(node_map[head.random]) if head.random is not None else "null") + "],"
~~~~~~~~^^^^^^^^^^^^^
KeyError: <main.Node object at 0x7ffff7672990>
Not able to understand this error and not able to see issue with my solution. Could you please check this ?