Leetcode0002--Add Two Numbers 链表求和
【转载请注明】http://www.cnblogs.com/igoslly/p/8672467.html
来看一下题目:
You are given two non-empty linked lists representing two non-negative integers. 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. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) |
题目意思其实就是将两个数,以链表逆序的方式进行存储 求和后,将结果也逆序输出 |
思路:
1、由于两个数的大小位数,链表 -> int数进行的想法基本无望,可能越界
2、链表已经逆序,已经提供很好的“对应位相加,向前进位”的运算模式
注意点:
1、List1和List2不等长
2、当某个List完成后,可单个计算另外的List
3、考虑进位关系,例如9+99999
4、最后进位时,需额外设定val=1的结点
实现方法1(初始):
方法1即是依照上面思路,依样画葫芦得到的结果
但其实细看,本题代码重复的部分太多,而且一直需要使用pre变量跟踪前结点,有些多余
/**
* 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) {
ListNode *p1=l1,*p2=l2,*pre=p1;
int up=; // 将结点l1作为结果链表返回,在l1和l2均在有效范围内,进行添加,同时更新up进位
while(p1&&p2){
p1->val+=p2->val+up;
up=p1->val/;
if(up==){
p1->val-=;
}
pre=p1;
p1=p1->next;
p2=p2->next;
} // 当l1结束后,pre最后个元素连接到l2后部
if(p1==NULL){
pre->next=p2;
while(p2!=NULL){
p2->val+=up;
up=p2->val/;
if(up==){
p2->val-=;
}
pre=p2;
p2=p2->next;
}
} // 当l2结束后,单独计算l1
if(p2==NULL){
while(p1!=NULL){
p1->val+=up;
up=p1->val/;
if(up==){
p1->val-=;
}
pre=p1;
p1=p1->next;
}
} // 当计算结束,up=1时,表示和多一位,新创建val=1的ListNode添加
if(up==){
pre->next=new ListNode();
}
return l1;
}
};
实现方法2 (对方法1进行优化):
相对看起来更加简洁
/**
* 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) {
int carry=;
ListNode* listNode=new ListNode();
ListNode* p1=l1,*p2=l2,*p3=listNode; // 修改判断条件从 && 到 ||
while(p1!=NULL||p2!=NULL)
{
// 在while循环里添加p1和p2的判断,省去了某个List完毕后单独List的情况
if(p1!=NULL)
{
carry+=p1->val;
p1=p1->next;
}
if(p2!=NULL)
{
carry+=p2->val;
p2=p2->next;
}
p3->next=new ListNode(carry%);
p3=p3->next;
carry/=;
} // 由于辟出了单独的result链表,故而无需再用pre继续前结点
if(carry==)
p3->next=new ListNode();
return listNode->next;
}
};
Leetcode0002--Add Two Numbers 链表求和的更多相关文章
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 002 Add Two Numbers 链表上的两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- Add Two Numbers(链表)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode 2 Add Two Numbers(链表)
数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...
- [LeetCode]2. Add Two Numbers链表相加
注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...
- 链表求和12 · Add Two Numbers
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
- LeetCode 445. Add Two Numbers II(链表求和)
题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...
随机推荐
- python中zip( )的使用
zip函数简单用法 x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x,y,z) #得到一个zip对象 xyz #打印结果为<zip ob ...
- Tomcat启动失败--Several ports (8005, 8080, 8009)
启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are alre ...
- everyday two problems / 3.11 - 3.17
7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...
- 华为/H3C Syslog配置
H3C交换机的设置举例1. 组网需求将系统的日志信息发送到 linux 日志主机:日志主机的IP 地址为1.2.0.1/16:信息级别高于等于 informational 的日志信息将会发送到日志主机 ...
- hdu_1014_Uniform Generator_201310141958
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hibernate分表保存日志
@Service("accessLogService")@Transactionalpublic class LogMessageServiceImpl extends BaseD ...
- Spring MVC-表单(Form)标签-单选按钮(RadioButton)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_radiobutton.htm 说明:示例基于Spring MVC 4.1.6. ...
- Clojure:解决selmer模板不刷新的问题
当在REPL环境中尝试调试template的时候,会发现每次都需要重启REPL才能看到最新的变化.调查后发现,原来是每次启动REPL的时候,原来的template文件都被放到了target目录中,这样 ...
- iOS中UITextView的操作技巧
刚才看了一篇textView实现placeholder的文章,有兴趣的同学们能够看下:__biz=MzA3NzM0NzkxMQ==&mid=211846438&idx=1&sn ...
- 使用python获取CPU和内存信息的思路与实现(linux系统)
linux里一切皆为文件,在linux/unix的根文件夹下,有个/proc文件夹,这个/proc 是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做"/proc&qu ...