* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
string solve(string s, string t)
{
// write code here
int len_s_ = s.size();
int len_t_ = t.size();
int jinwei=0;//进位
int gewei = 0;//个位
stack<char> res;
for(int i=len_s_-1,j=len_t_-1;i>=0||j>=0;i--,j--)
{
int s_n = '0',t_n='0';
if(i>=0) s_n = s[i];
if(j>=0) t_n = t[j];
int now = s_n-'0'+t_n-'0'+jinwei;
jinwei = now/10;
now%=10;
res.push(now+'0');
}
if(jinwei!=0) res.push(jinwei+'0');
string res_;
while(!res.empty())
{
res_.push_back(res.top());
res.pop();
}
return res_;
}
string int_generate_str(ListNode* p)
{
string res;
for(ListNode* tmp = p;tmp!=nullptr;tmp=tmp->next)
res+='0'+tmp->val;
return res;
}
ListNode* str_transfer_list(string str)
{
ListNode* res = new ListNode(-1);
ListNode* point = res;
for(char c:str)
{
ListNode* p=new ListNode(c-'0');
point->next = p;
point = point->next;
}
return res->next;
}
ListNode* addInList(ListNode* head1, ListNode* head2) {
return str_transfer_list(solve(int_generate_str(head1), int_generate_str(head2)));
}
};