Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * 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 n1=0;
        int n2 = 0;
        int data1 = 0; //l1->val;
        int data2 = 0; //l2->val;
        ListNode *p1 = l1;
        ListNode *p2 = l2;
        int mult = 1;
        ListNode *OutputNode = new ListNode();
        ListNode *p3 = OutputNode;
        
       
    while(p1!=NULL)
    {
        data1 = p1->val;
        n1 = data1*mult + n1;
        p1 = p1->next;
        mult = mult*10;
        
    }
        mult = 1;
    while(p2!=NULL)
    {
        data2 = p2->val;
        n2 = data2*mult + n2;
        p2 = p2->next;
        mult = mult *10;
    }
    
        int sum = n1+n2;
        int output = 0;
        while(sum!=0)
        {
            output = sum%10;
            p3->val = output;
            p3 = p3->next;
            sum = sum/10;
        }
        cout<<"n1: "<<n1<<endl<<"n2: "<<n2<<endl;
    return OutputNode;
    
    
    
        
        
    }
    
};




Error: solution.cpp: In member function addTwoNumbers
Line 19: Char 45: error: no matching function for call to 'ListNode::ListNode()'
ListNode *OutputNode = new ListNode();


How else do i create a new linked list?
How else do i create a new linked list?

Use a constructor that exists.
If you need one that takes no arguments, then make one.
Otherwise use the one that takes an int.
Topic archived. No new replies allowed.