Please tell me why is "Runtime Error"?


  • 0
    L
     I know my code is ugly.But I dont think it's wrong.Could tell me why?Plea
    
     /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
           
            int valuel1 = 0,valuel2 = 0,count = 1,sum = 0,num = 0;
            ListNode *temp = l1;
            while(temp){
                valuel1 += count * (temp->val);
                count = 10 * count;
                temp = temp->next;
            }
            temp = l2;count = 1;
            while(temp){
                valuel2 += count * (temp->val);
                count = 10 * count;
                temp = temp->next;
            }
            sum = valuel1 + valuel2;
            num = sum%10;
            sum = sum/10;
            ListNode head(num);
            ListNode *p,*q;
            p = &head;q = p;
            while(sum > 0){
                num = sum%10;
                ListNode result(num);
                q->next = &result;
                q =q->next;
                sum = sum/10;
            }
            return p;
        }
        
    };

  • 0
    L

    I know my code is ugly.But I dont think it's wrong.Could tell me why?Plea


  • 0
    W
    This post is deleted!

Log in to reply
 

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