[C]Runtime Error


  • 0
    C

    Below is my code, can anyone help me solve this "RUNTIME ERROR" problem?

    =========================
    int overflow = 0;

    int getListLen(struct ListNode *list)
    {
    int len = 0;
    struct ListNode *node = NULL;

    if (NULL == list) return len;
    
    node = list;
    while (node) {
    	++len;
    	node = node->next;
    }
    return len;
    

    }

    void addTwoList(struct ListNode *l1, struct ListNode *l2)
    {
    struct ListNode *p_l1 = l1, *p_l2 = l2, *p_l1_last = l1;

    if (getListLen(l1) < getListLen(l2)) return;
    
    while (p_l1 != NULL) { 
    	p_l1->val = p_l1->val + p_l2->val + overflow;
    	if (p_l1->val > 9) {
    		overflow = 1;
    		p_l1->val %= 10;
    	} else {
    		overflow = 0;
    	}
    
    	p_l1 = p_l1->next;
    	p_l2 = p_l2->next;
    }
    
    if (1 == overflow) {
        p_l1_last = p_l1->next;
        p_l1_last->val += overflow;
    }
    

    }

    struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
    {
    int l1_len = getListLen(l1);
    int l2_len = getListLen(l2);

    if (0 == l1_len || NULL == l1) return l2;
    if (0 == l2_len || NULL == l2) return l1;
    
    if (l1_len < l2_len) {
    	addTwoList(l2, l1);
    	return l2;
    } else {
    	addTwoList(l1, l2);
    	return l1;
    }
    

    }


  • 0
    L
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.