Leetcode 第 2 题(Add Two Numbers)

题目例如以下:

Question

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

这道题相对来说比較简单,考察的就是链表的一些最主要的操作。唯一须要注意是两个链表中的数都加完后,假设还有进位。须要再单独处理一下进位。

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode * pCurrent, * pNext, * pHead = NULL;
int carry = 0; while(l1 || l2)
{
pNext = new ListNode(0);
if(pHead == NULL)
{
pHead = pNext;
}
else
{
pCurrent->next = pNext;
}
pCurrent = pNext; int op1 = l1 ? l1->val : 0;
int op2 = l2 ? l2->val : 0;
int k = carry + op1 + op2; carry = k / 10;
pCurrent->val = k % 10; if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
if(carry != 0)
{
pNext = new ListNode(carry );
pCurrent->next = pNext;
}
return pHead;
}

这个代码看着有点长。主要是链表的头指针须要在生成链表的第一个元素的时候单独处理一下。也就是以下的这个代码片段:

if(pHead == NULL)
{
pHead = pNext;
}
else
{
pCurrent->next = pNext;
}

利用一些小技巧。这里能够写的更优雅一些。一个经常使用的技巧是在链表的开头设立一个“哨兵”节点。这个节点不存详细的数据。有了这个节点就不须要特殊的处理头指针了。有了这个“哨兵”节点,也就不须要头指针了。以下是改进后的代码:

ListNode* addTwoNumbers_2(ListNode* l1, ListNode* l2)
{
ListNode headNode(0);
ListNode *pCurrent = &headNode, *pNext;
int carry = 0; while(l1 || l2)
{
int op1 = l1 ? l1->val : 0;
int op2 = l2 ? l2->val : 0; int k = carry + op1 + op2;
carry = k / 10;
pNext = new ListNode(k % 10); pCurrent->next = pNext;
pCurrent = pNext; if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
if(carry != 0)
{
pNext = new ListNode(carry );
pCurrent->next = pNext;
}
return headNode.next;
}

至此,这个代码就看着比較简洁了。

除了在链表的开头能够加这么个“哨兵”。有时在链表的结尾加个“哨兵”也能简化代码。

比方说这个问题中,能够设置一个结尾哨兵。这个哨兵的值为 0, next 指针指向自己。

有这个“哨兵”之后,程序中间的 while 循环还能够简化。

可是将这个哨兵加到两个链表的结尾也是有开销的。

对这个样例,可能有点得不偿失。作为演示。我还是给出了代码。在真实的项目应用中,假设用到这样的方法,一定要在建立链表时就加入好这个结尾节点。

ListNode* addTwoNumbers_2(ListNode* l1, ListNode* l2)
{
ListNode headNode(0);
ListNode *pCurrent = &headNode, *pNext; ListNode tailNode(0);
tailNode.next = &tailNode; pNext = l1;
while(pNext->next) pNext = pNext->next;
pNext->next = &tailNode; pNext = l2;
while(pNext->next) pNext = pNext->next;
pNext->next = &tailNode; int carry = 0; while(l1 != &tailNode || l2 != &tailNode)
{
int k = carry + l1->val + l2->val;
carry = k / 10;
pNext = new ListNode(k % 10); l1 = l1->next;
l2 = l2->next;
pCurrent->next = pNext;
pCurrent = pNext; }
if(carry != 0)
{
pNext = new ListNode(carry );
pCurrent->next = pNext;
}
return headNode.next;
}

Leetcode 第 2 题(Add Two Numbers)的更多相关文章

  1. leetcode第四题--Add Two Numbers

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

  2. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  3. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

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

  4. LeetCode解题笔记 - 2. Add Two Numbers

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

  5. LeetCode第二题—— Add Two Numbers(模拟两数相加)

    Description: You are given two non-empty linked lists representing two non-negative integers. The di ...

  6. [算法题] Add Two Numbers

    题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...

  7. LeetCode(2)Add Two Numbers

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

  8. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

  9. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

随机推荐

  1. crm操作发票实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query;     using Microsoft.Cr ...

  2. PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自測4. Have Fun with Numbers (20) 【二星级】

    题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4 题面: 00-自測4. Have Fun with Numbers ...

  3. vim 常用插件功能跟配置

    在之前的公司,一直是使用别人配置好的vim 环境,他当时配置的功能很强大,查看源码的时候,非常的方便.至少我一直都是用它来看源码,从来没有使用过source insight.现在换了工作,但之前养成的 ...

  4. android 图片特效处理之光晕效果

    这篇将讲到图片特效处理的图片光晕效果.跟前面一样是对像素点进行处理,本篇实现的思路可参见android图像处理系列之九--图片特效处理之二-模糊效果和android图像处理系列之十三--图片特效处理之 ...

  5. js -- 分页功能

    html 代码 <html> <head> <meta charset='utf-8'> <script type="text/javascript ...

  6. vue中的插槽slot理解

    本篇文章参考赛冷思的个人博客 1.函数默认传参 在我们写js函数我们的可能会给他们一个默认的参数,写法是 function show(age,name){ var age = age || 20; v ...

  7. 关于li的排列,我的面试题

    来到北京的第二周,收到了单位的面试,一面的时候面试官问了微信钱包里的那个快速入口的排列,我当时在面试官的引导下答的还可以,但是在实际中有很多的方法和各自不同的问题,我来总结下. 1.flex布局,其实 ...

  8. Impala数据处理(加载和存储)

    不多说,直接上干货! Hive与Impala都是构建在Hadoop之上的数据查询工具,那么在实际的应用中,它们是如何加载和存储数据的呢? Hive和Impala存储和加载表,和所有的关系型数据库一样, ...

  9. Python 学习 第三天 课后总结:

    PYTHON学习第三天课后总结: 1,注释:就是对代码起到说明注解的作用.   注释分为单行注释与多行注释.  单行注释:只注释一行代码在需要注释的所在行的行首使用#号来注释此行,注意#与代码之间需要 ...

  10. 【例题 8-12 UVA-12627】Erratic Expansion

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 规律+递归题 f[k][i] k时刻前i行的红气球个数 i<=2^(k-1) f[k][i] = 2*f[k-1][i]; i ...