PYTHON CODE HELP

Issue with this code for same problem on leetcode as code template is not provided here

"""
# 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:
    # @param head, a RandomListNode
    # @return a RandomListNode
    def copyRandomList(self, head):
        
        temp=head
        while temp:
            new_node=Node(temp.val)
            new_node.next=temp.next
            temp.next=new_node
            temp=new_node.next
        temp=head
        while temp:
            a=temp
            a_dash=a.next
            d=a.random
            d_dash=d.next
            a_dash.random=d_dash
            temp=temp.next.next
        
        new_head=head.next
        t,t_dash=head,head.next
        while t and t_dash:
            t.next=t_dash.next
            t_dash.next=t.next.next
        return new_head

hey can u please elaborate on this

@Kartikkhariwal1 ode not working.
Where did i went wrong ?

Hey @ritvikagrawal1

"""
# Definition for a Node.
class Node:
    def __init__(self, x, next=None, random=None):
        self.val = int(x)
        self.next = next
        self.random = random
"""

class Solution:
    # @param head, a RandomListNode
    # @return a RandomListNode
    def copyRandomList(self, head):
        if(head==None): #added this
            return 
        temp=head
        while temp:
            new_node=Node(temp.val)
            new_node.next=temp.next
            temp.next=new_node
            temp=new_node.next
        temp=head
        while temp:
            a=temp
            a_dash=a.next
            d=a.random
            if(d!=None):  # added 
                d_dash=d.next
            else:  # added 
                d_dash=None  # added 
            a_dash.random=d_dash
            temp=temp.next.next
        new_head=head.next
        t,t_dash=head,head.next
        while t and t_dash:
            t.next=t_dash.next
            if(t_dash.next!=None): #added
                t_dash.next=t.next.next 
            else: #adde
                t_dash.next=None #added
            t=t.next #added
            t_dash=t_dash.next #added
        return new_head

@Kartikkhariwal1 Got it. Thank You!

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.