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.
/**
* 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();