Leetcode 第 2 题(Add Two Numbers)
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)的更多相关文章
- leetcode第四题--Add Two Numbers
Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode解题笔记 - 2. Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- [算法题] Add Two Numbers
题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
随机推荐
- 使用 Facebook开源动画库 POP 实现真实衰减动画
1. POP动画基于底层刷新原理.是基于CADisplayLink,1秒钟运行60秒,接近于游戏开发引擎 @interface ViewController () @property (nonatom ...
- es65 跨模块常量
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python-openpyxl安装
今天在安装openpyxl的时候,一直提示错误,后来才发现仅仅安装它还不够,还需要其他两个库的支持1.安装jdcal2.安装et_xmlfile这两个库安装的方法,都是直接在命令行下面,进入库文件se ...
- js插件---datatables如何使用
js插件---datatables如何使用 一.总结 一句话总结:a.引入css和js(不要忘记css):b.js代码启动插件(里面可以用参数控制各种功能) 1.dataTables如何显示控制行(比 ...
- 下载安装与配置Excel 2013数据挖掘加载项(SQL Server 2012 SP1 + SQLServer2012_DMAddin.msi)
一.系统要求 在安装这个数据挖掘加载项前,你的机器必须要可以支持如下的相关系统设备: (1)操作系统:windows 7 和 windows 8,暂时不支持 windows 10. (2)excel ...
- ActionListener三种实现
/** * Simple1.java - 处理事件的第一种方法 * 在这个例子中,利用一个ActionListener来监听事件源产生的事件 * 用一些if语句来决定是哪个事件源 */ import ...
- mount---挂载文件系统
挂载概念 Linux中的根目录以外的文件要想被访问,需要将其“关联”到根目录下的某个目录来实现,这种关联操作就是“挂载”,这个目录就是“挂载点”,解除次关联关系的过程称之为“卸载”. 注意:“挂载点” ...
- 03007_JDBC概述
1.JDBC概述 (1)JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...
- MyBatis学习总结(16)——Mybatis使用的几个建议
1.Mapper层参数为Map,由Service层负责重载. Mapper由于机制的问题,不能重载,参数一般设置成Map,但这样会使参数变得模糊,如果想要使代码变得清晰,可以通过service层来实现 ...
- 22. Node.Js Buffer类(缓冲区)-(二)
转自:https://blog.csdn.net/u011127019/article/details/52512242