首先,演示一个错误的reverList

 class Solution {
public:
ListNode* reverse(ListNode* root)
{
if(NULL == root)
return NULL;
ListNode* pCur = root;
ListNode* pNext = root->next; while(pNext)
{
pNext = pNext->next;
pCur->next->next = pCur;
pCur = pCur->next;
}
root->next = NULL;
return pCur;
} };

(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(-);
ListNode* pNew = &dummy;
int carry = ;
int sum = ; while(p1 && p2)
{
sum = (p1->val + p2->val + carry)%;
carry= (p1->val + p2->val + carry)/;
pNew->next = new ListNode(sum);
pNew = pNew->next;
p1 = p1->next;
p2 = p2->next;
} while(p1)
{
sum = (p1->val + carry)%;
carry= (p1->val + carry)/;
pNew->next = new ListNode(sum);
pNew = pNew->next;
p1 = p1->next;
} while(p2)
{
sum = (p2->val + carry)%;
carry= (p2->val + carry)/;
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 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  3. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  4. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  5. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  6. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  7. [Leetcode] Add two numbers 两数之和

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. [LeetCode] Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  9. LeetCode——Add Two Numbers

    Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...

随机推荐

  1. JCL: What is EXCP

      JCL: What is EXCP ?   EXCP stands for EXecute Channel Program. These are the I/O subsystem hardwar ...

  2. shell 文件个数 vs 文件夹个数

    文件个数 ls -l |grep "^-"|wc -l 文件夹个数 ls -l |grep "^d"|wc -l

  3. BZOJ 4883 [Lydsy2017年5月月赛]棋盘上的守卫(最小生成环套树森林)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4883 [题目大意] 在一个n*m的棋盘上要放置若干个守卫. 对于n行来说,每行必须恰好 ...

  4. 【后缀自动机】hihocoder1441 后缀自动机一·基本概念

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi:今天我们来学习一个强大的字符串处理工具:后缀自动机(Suffix Automaton,简称SAM).对于一个字符串 ...

  5. Problem C: 指针:自定义函数length,调用它计算字符串的长度

    #include<stdio.h> int length(char*s) { int i,count; while(*s!='\0') { *(s++); count++; } retur ...

  6. Mysql双主实战

    参考:http://dev.mysql.com/doc/refman/5.1/en/replication-options-slave.htmlhttp://blog.chinaunix.net/ui ...

  7. convirt介绍

    convirt2.0是一款使用python和jquery结合编写的其于web的集中管理xen服务的程序.该程序在xen 社区项目,管理项目中被使用的量很高,convirt开发有开源版本与企业版本,企业 ...

  8. Delphi 类引用 Class Reference 元类 MetaClass 用法

    delphi中类引用的使用实例 类引用类引用(Class Reference)是一种数据类型,有时又称为元类(MetaClass),是类的类型的引用.类引用的定义形式如下: class of type ...

  9. Migrating Oracle on UNIX to SQL Server on Windows

    Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Ap ...

  10. THttpClientSocket token验证

    THttpClientSocket uses SynCrtSock非WINHTTP.DLL里面的控件,可以用于手机端. function Client(const SQL: RawUTF8): Raw ...