[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
一个很自然的想法是先分别遍历两个链表,求得两个加数add1和add2,然后求得sum=add1+add2,最后再将sum用链表按位逆序存储。这种方法忽略了链表长度很大时数据并不能存储在int或其他整型类型里的问题。即此题应该注意是一个大数问题。
思路:建立一个新链表,然后把输入的两个链表从头往后遍历,每两个相加,添加一个新节点到新链表后面,注意需要处理进位问题。还有就是最高位的进位问题要最后特殊处理一下。时间复杂度O(n)。
class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *res = new ListNode(-1); ListNode *cur = res; int carry = 0; while (l1 || l2) { int n1 = l1 ? l1->val : 0; int n2 = l2 ? l2->val : 0; int sum = n1 + n2 + carry; carry = sum / 10; cur->next = new ListNode(sum % 10); cur = cur->next; if (l1) l1 = l1->next; if (l2) l2 = l2->next; } if (carry) cur->next = new ListNode(1); return res->next; } };
在CareerCup上的这道题还有个Follow Up,把链表存的数字方向变了,原来是表头存最低位,现在是表头存最高位。这样稍微麻烦一些。一种想法是既然存的数字方向相反,那么我们可以先分别将两个链表反转,即可得到和上述一样的问题,这个时间复杂度同样也是O(n):
class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *dummy = new ListNode(-1); ListNode *cur = dummy; int carry = 0; l1 = reverseList(l1); l2 = reverseList(l2); while (l1 || l2) { int n1 = l1 ? l1->val : 0; int n2 = l2 ? l2->val : 0; int sum = n1 + n2 + carry; carry = sum / 10; cur->next = new ListNode(sum % 10); cur = cur->next; if (l1) l1 = l1->next; if (l2) l2 = l2->next; } if (carry) cur->next = new ListNode(1); return reverseList(dummy->next); } ListNode *reverseList(ListNode *head) { if (!head) return head; ListNode *dummy = new ListNode(-1); dummy->next = head; ListNode *cur = head; while (cur->next) { ListNode *tmp = cur->next; cur->next = tmp->next; tmp->next = dummy->next; dummy->next = tmp; } return dummy->next; } };
第二种想法是先分别计算出两个链表的长度,然后给短一点的链表前面补0,补到和另一个链表相同的长度。由于要从低位开始相加,而低位是链表的末尾,所以我们采用递归来处理,先遍历到链表的末尾,然后从后面相加,进位标示符carry用的是引用,这样保证了再递归回溯时值可以正确传递,每次计算的节点后面接上上一次回溯的节点,直到回到首节点完成递归。最后还是处理最高位的进位问题:
class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { int n1 = 0, n2 = 0, carry = 0;; n1 = getLength(l1); n2 = getLength(l2); if (n1 > n2) l2 = padList(l2, n1 - n2); if (n2 > n1) l1 = padList(l1, n2 - n1); ListNode *res = addTwoNumbersDFS(l1, l2, carry); if (carry == 1) { ListNode *tmp = new ListNode(1); tmp->next = res; res = tmp; } return res; } ListNode *addTwoNumbersDFS(ListNode *l1, ListNode *l2, int &carry) { if (!l1 && !l2) return NULL; ListNode *list = addTwoNumbersDFS(l1->next, l2->next, carry); int sum = l1->val + l2->val + carry; ListNode *res = new ListNode(sum % 10); res->next = list; carry = sum / 10; return res; } ListNode *padList(ListNode *list, int len) { ListNode *dummy = new ListNode(-1); ListNode *cur = dummy; for (int i = 0; i < len; ++i) { cur->next = new ListNode(0); cur = cur->next; } cur->next = list; return dummy->next; } int getLength(ListNode *list) { ListNode *cur = list; int res = 0; while (cur) { ++res; cur = cur->next; } return res; } };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探