[LeetCode] Add Two Numbers(stored in List)
首先,演示一个错误的reverList
1 class Solution { 2 public: 3 ListNode* reverse(ListNode* root) 4 { 5 if(NULL == root) 6 return NULL; 7 ListNode* pCur = root; 8 ListNode* pNext = root->next; 9 10 while(pNext) 11 { 12 pNext = pNext->next; 13 pCur->next->next = pCur; 14 pCur = pCur->next; 15 } 16 root->next = NULL; 17 return pCur; 18 } 19 20 };
(2)--------->(3)-------->(4)----------->(5)--------->NULL
首先pCur指向2,pNext指向3;
pNext=pNext->next; pNext指向4,
pCur->next->next = pCur,然后3--->4 的指针断了, 从此pCur就自己转圈了。。。
正确的reverseList
ListNode * reverseList(ListNode* head) {if(head == NULL) return NULL;ListNode *pre = NULL;ListNode *cur = head;ListNode *next = NULL;while(cur){next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}
这个题目也不难,注意dummy节点的使用,另外,记得最后carrybit的处理
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode *addTwoNumbers(ListNode *l1, ListNode *l2){if(l1 == NULL)return l2;if(l2 == NULL)return l1;ListNode* p1 = l1;ListNode* p2 = l2;ListNode dummy(-1);ListNode* pNew = &dummy;int carry = 0;int sum = 0;while(p1 && p2){sum = (p1->val + p2->val + carry)%10;carry= (p1->val + p2->val + carry)/10;pNew->next = new ListNode(sum);pNew = pNew->next;p1 = p1->next;p2 = p2->next;}while(p1){sum = (p1->val + carry)%10;carry= (p1->val + carry)/10;pNew->next = new ListNode(sum);pNew = pNew->next;p1 = p1->next;}while(p2){sum = (p2->val + carry)%10;carry= (p2->val + carry)/10;pNew->next = new ListNode(sum);pNew = pNew->next;p2 = p2->next;}if(carry){pNew->next = new ListNode(carry);pNew = pNew->next;} return dummy.next;}};
[LeetCode] Add Two Numbers(stored in List)相关推荐
- LeetCode Add Two Numbers II(栈)
问题:给出两个链表表示的整数,求其和 思路:因为链表的第一个结点是最高位,最后一个结点是最低位.先将两个链表放入两个栈中.然后从两个栈中取出元素,从低到高位相加. 具体代码参考: https://gi ...
- 每日一则 LeetCode: Add Two Numbers
描述 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode:Add Two Numbers
题目链接 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- [LeetCode] Add Two Numbers
题目链接: https://oj.leetcode.com/problems/add-two-numbers/ 问题: You are given two linked lists represent ...
- leetcode:Add Two Numbers(java)
package Leetcode;/*** 题目:* You are given two non-empty linked lists representing two non-negative in ...
- LeetCode Add Two Numbers
题意:链表加法 代码如下: class Solution {ListNode addTwoNumbers(ListNode l1, ListNode l2){ListNode cur1 = l1, c ...
- leetcode add Two Numbers
部分 conditional operators ?:写的statements 在有的编译器下能通过,有的可能通不过 base operand of '->' has non-pointer ...
- LeetCode之Add Two Numbers
LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...
- LeetCode题解:Add Two Numbers
LeetCode题解 说明:本人不是什么算法高手,部分内容参考了Google.stackoverflow.segmentfault中得到的一些解答或者代码.之所以去做Leetcode上的题是因为毕业工 ...
最新文章
- 第十六节,使用函数封装库tf.contrib.layers
- FPGA之道(58)关于外界接口的编程思路
- Android中Google Drive显示黑屏问题分析
- CentOS服务器iptables配置
- 学习响应式BootStrap来写融职教育网站,Bootsrtap第十天你的收获
- SEO技巧:好的域名是网站成功的开始
- java中类似webapi,在.net框架应用程序中包含.net核心WebAPI?
- linux得到低权shell,oracle低权限下获取shell
- java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0特殊字符表达
- react-native 自定义 下拉刷新 / 上拉加载更多 组件
- 玲珑杯round11-B:萌萌哒的第二题
- selenium调用浏览器打印功能,并保存为PDF
- uniApp微信小程序获取当前用户手机号码(前端)
- sin18度用计算机怎么算,sin18度等于多少怎么计算
- MySQL中幻读现象
- 几个高质量无版权图库,还愁找不到配图?
- python 构件二维数组_通过这四个构件块来升级您的javascript数组
- 程序员能有什么好的出路?
- 交易猫源码搭建完整版
- AI治退“七天无理由退换货”
热门文章
- 试验OSPF域内IP地址冲突会造成的安全问题
- redistemplate.opsforhash设置过期时间_Redis详解(十一)------ 过期删除策略和内存淘汰策略...
- 0基础学习数据分析必须掌握的技能有哪些?
- android课程设计录音机,[转载]数字录音机(微机原理与接口技术-课程设计)
- 为什么颜值越来越重要_看脸时代来了吗?为什么这个时代越来越注重颜值?
- 单片机C语言知识用法之,单片机C语言知识用法之define
- python的HTML文件中使用和加载CSS文件
- unity 角色 动画脚本_Unity Animation --动画剪辑(外部来源的动画)
- 置换群和Burnside引理,Polya定理
- 解题报告:POJ - 1062 昂贵的聘礼(最短路、超级源点)