Python one passage of linked lists


#1

Definition for singly-linked list.

class ListNode:
def init(self, x):
self.val = x
self.next = None

class Solution:
# @param A : head node of linked list
# @param B : head node of linked list
# @return the head node in the linked list
def addTwoNumbers(self, A, B):
carry=0

    if(A is None and B is None):
        return None
    i=0
    new_C_node=None
    prev_C_node=None
    
    a_val_or_0=A.val if A!=None else 0
    b_val_or_0=B.val if B!=None else 0
    cc=carry+ a_val_or_0+b_val_or_0
    carry=(cc)/10
    head_C_node=ListNode((cc)%10)
    prev_C_node=head_C_node
    if(B!=None):
        B=B.next
    if(A!=None):
        A=A.next
    while(A!=None or B!=None):
        a_val_or_0=A.val if A!=None else 0
        b_val_or_0=B.val if B!=None else 0
        cc=carry+ a_val_or_0+b_val_or_0
        carry=(cc)/10
        if(B!=None):
            B=B.next
        if(A!=None):
            A=A.next
        
        new_C_node=ListNode((cc)%10)
        prev_C_node.next=new_C_node
        prev_C_node=new_C_node
      
    while(carry!=0):
        new_C_node=ListNode((carry)%10)
        carry=carry/10
        prev_C_node.next=new_C_node
        prev_C_node=new_C_node
    
    return head_C_node
    """
    if(A is None and B is None and carry==0):
        return None
    return C
    """
"""def addTwoNumbers(self, A, B,carry=0):
    
    if(A is None and B is None and carry==0):
        return None
    if(A is None and B is None):
        return ListNode(carry)
        
    a_val_or_0=A.val if A!=None else 0
    b_val_or_0=B.val if B!=None else 0
    cc=carry+ a_val_or_0+b_val_or_0
    carry=(cc)/10
    if(B!=None):
        B=B.next
    if(A!=None):
        A=A.next
    C=ListNode((cc)%10)
    C.next=self.addTwoNumbers(A,B,carry)
    return C 
"""