首先,演示一个错误的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)相关推荐

  1. LeetCode Add Two Numbers II(栈)

    问题:给出两个链表表示的整数,求其和 思路:因为链表的第一个结点是最高位,最后一个结点是最低位.先将两个链表放入两个栈中.然后从两个栈中取出元素,从低到高位相加. 具体代码参考: https://gi ...

  2. 每日一则 LeetCode: Add Two Numbers

    描述 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  3. LeetCode:Add Two Numbers

    题目链接 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  4. [LeetCode] Add Two Numbers

    题目链接: https://oj.leetcode.com/problems/add-two-numbers/ 问题: You are given two linked lists represent ...

  5. leetcode:Add Two Numbers(java)

    package Leetcode;/*** 题目:* You are given two non-empty linked lists representing two non-negative in ...

  6. LeetCode Add Two Numbers

    题意:链表加法 代码如下: class Solution {ListNode addTwoNumbers(ListNode l1, ListNode l2){ListNode cur1 = l1, c ...

  7. leetcode add Two Numbers

    部分 conditional operators  ?:写的statements 在有的编译器下能通过,有的可能通不过 base operand of '->' has non-pointer ...

  8. LeetCode之Add Two Numbers

    LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...

  9. LeetCode题解:Add Two Numbers

    LeetCode题解 说明:本人不是什么算法高手,部分内容参考了Google.stackoverflow.segmentfault中得到的一些解答或者代码.之所以去做Leetcode上的题是因为毕业工 ...

最新文章

  1. 第十六节,使用函数封装库tf.contrib.layers
  2. FPGA之道(58)关于外界接口的编程思路
  3. Android中Google Drive显示黑屏问题分析
  4. CentOS服务器iptables配置
  5. 学习响应式BootStrap来写融职教育网站,Bootsrtap第十天你的收获
  6. SEO技巧:好的域名是网站成功的开始
  7. java中类似webapi,在.net框架应用程序中包含.net核心WebAPI?
  8. linux得到低权shell,oracle低权限下获取shell
  9. java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0特殊字符表达
  10. react-native 自定义 下拉刷新 / 上拉加载更多 组件
  11. 玲珑杯round11-B:萌萌哒的第二题
  12. selenium调用浏览器打印功能,并保存为PDF
  13. uniApp微信小程序获取当前用户手机号码(前端)
  14. sin18度用计算机怎么算,sin18度等于多少怎么计算
  15. MySQL中幻读现象
  16. 几个高质量无版权图库,还愁找不到配图?
  17. python 构件二维数组_通过这四个构件块来升级您的javascript数组
  18. 程序员能有什么好的出路?
  19. 交易猫源码搭建完整版
  20. AI治退“七天无理由退换货”

热门文章

  1. 试验OSPF域内IP地址冲突会造成的安全问题
  2. redistemplate.opsforhash设置过期时间_Redis详解(十一)------ 过期删除策略和内存淘汰策略...
  3. 0基础学习数据分析必须掌握的技能有哪些?
  4. android课程设计录音机,[转载]数字录音机(微机原理与接口技术-课程设计)
  5. 为什么颜值越来越重要_看脸时代来了吗?为什么这个时代越来越注重颜值?
  6. 单片机C语言知识用法之,单片机C语言知识用法之define
  7. python的HTML文件中使用和加载CSS文件
  8. unity 角色 动画脚本_Unity Animation --动画剪辑(外部来源的动画)
  9. 置换群和Burnside引理,Polya定理
  10. 解题报告:POJ - 1062 昂贵的聘礼(最短路、超级源点)