LeetCode2: Add Two Numbers

2024-01-20 08:08
文章标签 numbers two add leetcode2

本文主要是介绍LeetCode2: Add Two Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

思路:同时遍历两个链表,超过10的需要进位,如果最后一位也超过10了,需要将结果链表多扩展一位,如果链表长度不一样,需要对没遍历完的链表继续处理完。

比如:

2->4->9

5->6->4

==>7->0->4->1

代码:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode p =l1;ListNode q =l2;int base=0;ListNode head = new ListNode(-1);ListNode tmp =head;while(p!=null&&q!=null){int val = p.val+q.val+base;ListNode node = new ListNode(val%10);tmp.next=node;tmp=node;base = val/10;p=p.next;q=q.next;}while(p!=null){int val = p.val+base;ListNode node = new ListNode(val%10);tmp.next=node;tmp=node;base = val/10;p=p.next;}while(q!=null){int val = q.val+base;ListNode node = new ListNode(val%10);tmp.next=node;tmp=node;base = val/10;q=q.next;}if(base>0){ListNode  node= new ListNode(base);tmp.next=node;tmp=node;}tmp.next=null;return head.next;}

 

这篇关于LeetCode2: Add Two Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/625320

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.B

一个bug日志 FATAL EXCEPTION: main03-25 14:24:07.724: E/AndroidRuntime(4135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syyx.jingubang.ky/com.anguotech.android.activity.Init

ubuntu16.04 Git add 使用tab键卡死

以前使用Ubuntu14.04 进行git add 操作时使用TAB键可以很快自动补全,但自从使用16.04使用TAB半天没有反应。 一开始以为是Git版本问题,后验证与Git无关。 搜索发现与Dash有关,以下是博客原文: http://www.51testing.com/html/50/n-1245050.html 今天碰到一个问题git 后面的参数用Tab键无法补全

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

Add All -uva优先队列的应用

题目的解法属于贪心,因为cost=a1+a2,所以要保证每次的cost最小,所以说,每次将队列中最小的两个相加,得出来的数放入队列中,再取2个最小的相加,直到全部加完,所以这就涉及了一个取2个最小数的问题,我说一下我一开始的做法 #include<stdio.h>#include<iostream>#include<stdlib.h>using namespace std;#define

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

Android studio jar包多层嵌套,Add library '__local_aars__:...@jar' to classpath问题

在添加jar包,早app下的build.gradle中的 implementation files('libs/jar包的名字.jar') 修改为 api files('libs/jar包的名字.jar') implementation 单层引用,只引用当前jar包层, api 多层引用,应用当前jar包层,已经jar包引用的jar包层

unable to access android sdk add-on list解决办法

mac环境,由于不小心删掉了sdk文件夹的内容,拷贝别人的文件内容过来后,发现sdkmanager不见了。 慌乱中重装了Android Studio。 打开app后发现如下提示:unable to access android sdk add-on list 解决办法: 在 Android Studio 安装目录 bin/idea.properties 文件最后追加一句 disabl

GIT:git add命令指定文件夹

git add命令可以指定文件夹来添加文件到git仓库中。 1、使用相对路径         当你在命令行中使用git add命令时,可以通过相对路径指定文件夹。例如,如果你的文件夹名为myfolder,可以使用以下命令将整个文件夹添加到git仓库中: git add myfolder/ 注意,路径名后面的斜杠是必需的,它表示将文件夹中的所有文件都添加。如果不加斜杠,命令会视为添加具