首先,演示一个错误的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. oracle substr

    SUBSTR( string, start_position [, length ] ) Parameters or Arguments string The source string. start ...

  2. Python开发基础-Day24socket套接字基础2

    基于UDP的socket 面向无连接的不可靠数据传输,可以没有服务器端,只不过没有服务器端,发送的数据会被直接丢弃,并不能到达服务器端 #客户端 import socket ip_port=('127 ...

  3. 解决phpStudy启动网站警告问题

    在用phpStudy的时候,在页面中会有一些警告 notice:Undefined variable... notice:Undefined index... 在php.ini里面找到 display ...

  4. BZOJ 1109 [POI2007]堆积木Klo(树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1109 [题目大意] Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体. ...

  5. 浅谈分布式CAP定理

    互联网发展到现在,由于数据量大.操作并发高等问题,大部分网站项目都采用分布式的架构.而分布式系统最大的特点数据分散,在不同网络节点在某些时刻(数据未同步完,数据丢失),数据会不一致. 在2000年,E ...

  6. Create process in UNIX like system

    In UNIX, as we’ve seen, each process is identified by its process identifier, which is a unique inte ...

  7. Smart config风险分析与对策

    Smart config风险分析与对策 1.简介:          Smart config是一种将未联网设备快速连接wifi的技术,大概原理如下图所示: 2.业务需求:          要求实现 ...

  8. 从connect到express01-connect

    介绍 Connect是一个node中间件框架.每个中间件在http处理过程中通过改写request, response的数据.状态,实现了特定的功能. 根据中间件在整个http处理流程的位置,将中间件 ...

  9. ArcGIS10.1如何将数据库注册到ARCSERVER服务器

    原文链接:http://www.cnblogs.com/hanchan/archive/2013/09/24/3337034.html 一.了解ArcGIS Server以及如何利用ArcServer ...

  10. openGl 基础

    最近由于手机项目中需要用到OpenGL ES的知识,所以这段时间正在研究OpenGL的相关知识.因为OpenGL ES是OpenGL的剪裁版本,所以我直接从OpenGL入手,然后再去看OpenGL E ...