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:基本加法规则 根据小学学的基本加法 ...
随机推荐
- mysql 内置功能 函数 date_format函数
创建数据库db12 create database db12 charset=utf8; use db12; 准备表和记录 CREATE TABLE blog ( id INT PRIMARY KEY ...
- java 多线程 day05 线程范围内的数据共享
import java.util.HashMap;import java.util.Map;import java.util.Random;/** * Created by chengtao on 1 ...
- Python(名称空间、函数嵌套、函数对象)
一.名称空间: 名称空间 定义:存放名字和值的绑定关系 内置名称空间 python自带的名字,如print.int.str 解释器启动就会生效 全局名称空间 文件级别定义的名字,都会放 ...
- uva 11426 GCD - Extreme (II) (欧拉函数打表)
题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...
- Jsoup学习总结
Jsoup学习总结 摘要 Jsoup是一款比较好的Java版HTML解析器.可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方 ...
- jst格式化日期
jsp页面需引入fmt标签:<taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"> ...
- 理解音视频 PTS 和 DTS
视频 视频的播放过程可以简单理解为一帧一帧的画面按照时间顺序呈现出来的过程,就像在一个本子的每一页画上画,然后快速翻动的感觉. 但是在实际应用中,并不是每一帧都是完整的画面,因为如果每一帧画面都是完整 ...
- 如何选择单片机和Android-LInux-ARM开发板?
源: 如何选择单片机和Android-LInux-ARM开发板?
- oracle 11g的卸载
oracle 11g 的卸载主要有两种方式:一种是使用Oracle Universal Installer管理工具,该工具以向导模式进行,比较简单.这里主要讲解第二种卸载数据库的方式-----使用”d ...
- Java_Chapter6_类与对象
6.1 类与对象的概念 6.2 定义类 class Cylinder { double radius; int height; double pi; void setCylinder(double r ...