LeetCode OJ:Add Two Numbers (相加链表之数)
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
将两个链表上的数相加就可以,大于10进一位,注意下相加时候的细节就可以了,我这里
吧prev节点记录下来,这样最后多生成节点的时候便于将最后一个多余的节点删除:
/**
* 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 * curr = new ListNode();
ListNode * root = curr;
ListNode * prev = curr;
int currVal = ;
while(l1 != NULL && l2 != NULL){
currVal = l1->val + l2->val;
curr->val += currVal;
curr->next = new ListNode(curr->val/);
curr->val%=;
prev = curr;
curr = curr->next;
l1 = l1->next;
l2 = l2->next;
}
while(l1 != NULL){
curr->val += l1->val;
curr->next = new ListNode(curr->val/);
curr->val %= ;
prev = curr;
curr = curr->next;
l1 = l1->next;
}
while(l2 != NULL){
curr->val += l2->val;
curr->next = new ListNode(curr->val/);
curr->val %= ;
prev = curr;
curr = curr->next;
l2 = l2->next;
}
if(curr->val == ){
prev->next = NULL;
delete curr;
}
return root;
}
};
感觉写的有点麻烦,应该有很多的重复代码可以改正,但是我暂时找不出来了,先这样吧。
更新下,以前脑子抽了写出了那样的代码。 其实三个while循环都可以放到一个while中, 下面用java在写一起,方法还是类似的:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode tmp = new ListNode(0);
ListNode head = tmp;
int carry = 0;
while(l1!=null || l2!=null || carry != 0){
int val = ((l1 != null)?l1.val:0) + ((l2!=null)?l2.val:0) + carry;
carry = carry/10 + val/10;
val %= 10;
tmp.next = new ListNode(val);
tmp = tmp.next;
l1 = l1!=null ? l1.next : l1;
l2 = l2!=null ? l2.next : l2;
}
return head.next;
}
}
LeetCode OJ:Add Two Numbers (相加链表之数)的更多相关文章
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- LeetCode 2 Add Two Numbers(链表操作)
题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
随机推荐
- macos没有任何来源怎么解决?
打开终端,输入如下命令即可: sudo spctl --master-disable
- ansible判定文件或者文件夹是否存在
ansible 的常用模块中没有判定当文件存在或者不存在时,执行某个执行 使用下面方法能简单判定某个文件是否存在 --- - name: judge a file or dir is exits sh ...
- Python一些常用模块
阅读目录 一: collections模块 二: time,datetime模块 三: random模块 四: os模块 五: sys模块 六: json,pickle 七: re正则模块 八:re模 ...
- 连接postgresql
# psycopg2 engine=create_engine('postgresql+psycopg2://scott:tiger@localhost/mydatabase')# python 连 ...
- python2 跟3的区别
1----python2:1 臃肿 , 源码的重复量很多2:语法不清晰,掺杂着 c,pyp,java,的一些陋习 python3: 几乎是重构后的源码,规范 清晰 优美 2.python的分类 分为编 ...
- webdeploy启用备份 msdeploy 启用backup
前提准备:完整安装Microsoft Web Deploy 3 下载页面:WebDeploy_amd64_zh-CN.msi msdeploy 同步站点 命令所在目录C:\Program Files\ ...
- oracle 创建视图、修改视图、删除视图、利用视图操作基本表
转:http://blog.sina.com.cn/s/blog_6b58d2fa0100rgvw.html 1.使用create or replace view命令创建视图 语法格式: create ...
- Java并发之CyclicBarria的使用(二)
Java并发之CyclicBarria的使用(二) 一.简介 之前借助于其他大神写过一篇关于CyclicBarria用法的博文,但是内心总是感觉丝丝的愧疚,因为笔者喜欢原创,而不喜欢去转载一些其他的文 ...
- Sybase:解锁
Sybase:解锁 Sql代码: --查询锁表 sp_iqlocks --解除锁定 drop connection[连接序号]
- Linux yum源地址
----------------------------------Linux yum源地址------------------------------ Zabbix 3.0 yum源 rpm -iv ...