Leetcode刷题总结: 445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7
思路一:
题目说了代表两个int型的数,第一个思路是我直接将这个list转成int型的正整数, 然后两个int型相加,为了防止溢出,用long存储sum;
再将sum转换成list;
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: unsigned int getNum(ListNode *pnode) { unsigned int result = 0; while(pnode != NULL) { result = result*10 + pnode->val; pnode = pnode->next; } return result; } ListNode* transNumToList(long num) { ListNode *cur = new ListNode(num%10); num = num/10; while (num > 0) { int res = num%10; num /= 10; ListNode *pnew = new ListNode(res); pnew->next = cur; cur = pnew; } return cur; } ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { unsigned int num1 = getNum(l1); unsigned int num2 = getNum(l2); cout << num1 << endl; cout << num2 << endl; unsigned long sum = num1+num2; return transNumToList(sum); } };
在LeetCode上测试,不通过,原因是输入的两个正整数并不是int范围的,数字范围可以是无穷大的;所以不能用直接相加的方式来完成
思路二:
只能用链表逐个相加的方式;因为题目说了没有以0开头的除非元素本身就是零,所以
1. 分别计算list1和list2的长度为len1和len2, 用vector<int> sum保存和的各个位,则sum的size最大为maxLen+1, pLarge指向l1和l2中len大的listnode, pSmall指向l1和l2中len小的listnode;
2. 用一个指针先走lendiff=|len2-len1|步,则sum[0...lendiff] = pLarge[0...lendiff]
3. temp = pLarge+pSmall;如果temp>10,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: int calcListLen(ListNode *pnode) { int len = 0; while(pnode != NULL) { ++len; pnode = pnode->next; } return len; } void addOneToSum(vector<int> &sum, int index) { for (int i = index; i>=0; --i) { if (sum[i] == 9) { sum[i] = 0; }else { sum[i] += 1; break; } } } ListNode *transToList(const vector<int> &sum) { ListNode *pPrev = new ListNode(-1); ListNode *pRoot = pPrev; if (sum[0] > 0) { pPrev->next = new ListNode(sum[0]); pPrev = pPrev->next; } for (int i = 1; i < sum.size(); ++i) { pPrev->next = new ListNode(sum[i]); pPrev = pPrev->next; } return pRoot->next; } ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int len1 = calcListLen(l1); int len2 = calcListLen(l2); int lendiff = 0; ListNode *pLarge = NULL; ListNode *pSmall = NULL; if (len1 > len2) { pLarge = l1; pSmall = l2; lendiff = len1 -len2; }else { pLarge = l2; pSmall = l1; lendiff = len2 - len1; } int maxLen = max(len1, len2); vector<int> sum(maxLen+1, 0); int sumIndex = 1; //sum[0]存放进位,当存在的时候 for (int i = 0; i < lendiff; ++i) { sum[sumIndex++] = pLarge->val; pLarge = pLarge->next; } while (pLarge != NULL) { int temp = pLarge->val + pSmall->val; if (temp >= 10) { sum[sumIndex] = temp%10; addOneToSum(sum, sumIndex-1); } else { sum[sumIndex] = temp; } ++sumIndex; pLarge = pLarge->next; pSmall = pSmall->next; } return transToList(sum); } };