链式A+B
来源:互联网 发布:我的世界村民交易js 编辑:程序博客网 时间:2024/06/05 05:18
</pre>题目描述</h2><div class="subject-question" style="font-size:14px; line-height:1.6; margin-bottom:20px; color:rgb(51,51,51); font-family:arial,STHeiti,"Microsoft YaHei",宋体"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.4em">有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。</p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.4em">给定两个链表ListNode* <span style="color:rgb(0,0,0)">A</span>,ListNode* <span style="color:rgb(0,0,0)">B</span>,请返回A+B的结果(ListNode*)。</p><div class="“subject-test-case-sample”">测试样例:</div><div class="“subject-test-case-sample”"><pre style="margin-top:0.8em; margin-bottom:0.8em; padding:10px 0px 10px 10px; font-size:14px; position:relative; border-left:2px solid rgb(26,188,156); background:rgb(254,254,254); word-break:break-all; font-family:Courier,"Courier New",monospace; white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51)">{1,2,3},{3,2,1}
返回:{4,4,4}
------------------------------------------------------思路:从个位加起,关键点在于进位,另外不new新节点的话,要注意两数位数不同时的指针连接。。。。。。/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {}};*/class Plus {public: ListNode* plusAB(ListNode* a, ListNode* b) { // write code here if (!a) return b; if (!b) return a; ListNode* c = a; int add = 0; int carry = 0;//jin wei ListNode* apre = nullptr; ListNode* bpre = nullptr; while (a && b){ add = carry + a->val + b->val; if (add >= 10){ add %= 10; carry = 1; }else { carry = 0; } a->val = add; if (!a->next && !b->next && carry){ ListNode* node = new ListNode(1); a->next = node; return c; }else { apre = a; a = a->next; bpre = b; b = b->next; } } if (!a) a = b; while (a){ add = carry + a->val; if (add >= 10){ add %= 10; carry = 1; }else { carry = 0; } a->val = add; apre->next = a; apre = a; if (a->next) a = a->next; else if (carry){ ListNode* node = new ListNode(1); a->next = node; a = nullptr; }else a = nullptr; } return c; }};
快慢指针和栈实现:/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {}};*/class Palindrome {public: bool isPalindrome(ListNode* pHead) { // 快慢指针和栈stack ListNode* fast = pHead; ListNode* slow = pHead; stack<int> s; while (fast && fast->next){ s.push(slow->val); fast = fast->next->next; slow = slow->next; } if (fast) //元素为奇数 slow = slow->next;//跳过中间节点 while (slow){ if (s.top()!=slow->val) return false; else { slow = slow->next; s.pop(); } } return true; }};
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {}};*/class Plus {public: ListNode* plusAB(ListNode* a, ListNode* b) { // write code here if (!a) return b; if (!b) return a; ListNode* c = a; int add = 0; int carry = 0;//jin wei ListNode* apre = nullptr; ListNode* bpre = nullptr; while (a && b){ add = carry + a->val + b->val; if (add >= 10){ add %= 10; carry = 1; }else { carry = 0; } a->val = add; if (!a->next && !b->next && carry){ ListNode* node = new ListNode(1); a->next = node; return c; }else { apre = a; a = a->next; bpre = b; b = b->next; } } if (!a) a = b; while (a){ add = carry + a->val; if (add >= 10){ add %= 10; carry = 1; }else { carry = 0; } a->val = add; apre->next = a; apre = a; if (a->next) a = a->next; else if (carry){ ListNode* node = new ListNode(1); a->next = node; a = nullptr; }else a = nullptr; } return c; }};
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {}};*/class Palindrome {public: bool isPalindrome(ListNode* pHead) { // 快慢指针和栈stack ListNode* fast = pHead; ListNode* slow = pHead; stack<int> s; while (fast && fast->next){ s.push(slow->val); fast = fast->next->next; slow = slow->next; } if (fast) //元素为奇数 slow = slow->next;//跳过中间节点 while (slow){ if (s.top()!=slow->val) return false; else { slow = slow->next; s.pop(); } } return true; }};
0 0
- 链式A+B
- 链式A+B
- 牛客网 | 链式A+B
- 链式A+B
- 链式A+B
- 链式A+B
- 链式A+B
- 算法2.5:链式A+B
- 2.Add Two Numbers&链式A+B
- 《程序员面试金典》链式A+B
- 链式A+B之程序员面试经典
- 链式A+B(解题报告)
- [程序员面试金典]-链式A+B
- (链式存储)比较两个字符串A和B大小
- 程序员面试金典 2.5 链式A+B
- 1.2链式结构 实现 C = A U B
- 程序员面试金典题解 链式A+B
- 第五天 链式A+B +回文链表
- 洛谷 1814
- js混合模式继承出现问题的解决
- yii,join l联查,及分页,最简单的jquery,全选反选,批删
- SVN 分支合并到分支
- springMVC <mvc:view-controller />
- 链式A+B
- iOS - 适配iOS10以及由于权限crash的问题
- Yii2 使用 Joins 查询
- app开发价格如何计算?
- TCP/IP详解学习笔记(12)-TCP的超时与重传
- Android中的动态加载机制--薛彦顺
- 读取android手机设置的参数 (getprop )
- 文章标题
- HTML_01_基础标签格式