# 川山甲

 博客园  :: 首页  ::  :: 联系 :: 订阅  :: 管理

输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)

#include <vector>
#include <map>
#include <iostream>
#include <math.h>
/**
*

*/
using namespace std;

struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL){}
};

template<class T>
int length(T& arr) {
return sizeof(arr)/ sizeof(arr[0]);
}

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
uint64_t carry = 0, sum = 0;

while (l1 || l2) {
sum =  (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
sum = carry / 10;
p->next  = new ListNode(sum % 10);

l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
p = p->next;
}
}
};

int main(int argc, char** argv){
int a[] = {2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9};
ListNode* tmp = new ListNode(0);
ListNode* ptr1 = tmp;
for (int i = 0; i < length(a); i++) {
ptr1->next = new ListNode(a[i]);
ptr1 = ptr1->next;
}
ptr1 = tmp->next;
delete tmp;

tmp = new ListNode(0);
int b[] = {5,6,4,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9,9,9,9};
ListNode* ptr2 = tmp;
for (int j = 0; j < length(b); j ++) {
ptr2->next = new ListNode(b[j]);
ptr2 = ptr2->next;
}
ptr2 = tmp->next;
delete tmp;

Solution* solution = new Solution();
delete solution;
return 0;
}


posted on 2018-05-10 14:32  川山甲  阅读(583)  评论(0编辑  收藏  举报