【JAVA、C++】LeetCode 002 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
解题思路:
定义三个ListNode l1、l2,result,其中result为return语句的输出,l1、l2为传入的参数。
将l1赋值给result,执行result.val+=l2.val,然后l1作为指针一级一级往下走,直到走到l2.next为null。当然,之间会有不少边界条件,自己debug一下就好。
Java代码如下:
public class Solution {
static public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result=l1;
while(true){
l1.val+=l2.val;
if(l1.val>=10){
l1.val%=10;
if(l1.next==null) l1.next=new ListNode(1);
else l1.next.val+=1;
}
if(l2.next==null){
ListNode l3=l1.next;
while(true){
if (l3==null) break;
if(l3.val==10){
l3.val%=10;
if(l3.next==null) l3.next=new ListNode(1);
else l3.next.val+=1;
}
l3=l3.next;
}
break;
}
l2=l2.next;
if(l1.next==null){
l1.next=new ListNode(0);
}
l1=l1.next;
}
return result;
}
}
C++
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* res=l1;
while (true) {
l1->val += l2->val;
if (l1->val >= ) {
l1->val %= ;
if (l1->next == NULL)
l1->next = new ListNode();
else l1->next->val += ;
}
if (l2->next == NULL) {
ListNode* l3 = l1->next;
while (true) {
if (l3 == NULL) break;
if (l3->val == ) {
l3->val %= ;
if (l3->next == NULL) l3->next = new ListNode();
else l3->next->val += ;
}
l3 = l3->next;
}
break;
}
l2 = l2->next;
if (l1->next == NULL) {
l1->next = new ListNode();
}
l1 = l1->next;
}
return res;
}
};
【JAVA、C++】LeetCode 002 Add Two Numbers的更多相关文章
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 001 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【JAVA、C++】LeetCode 018 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【JAVA、C++】LeetCode 015 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
随机推荐
- 【bzoj1013】 JSOI2008—球形空间产生器sphere
www.lydsy.com/JudgeOnline/problem.php?id=1013 (题目链接) 题意 有一个n维的球体,给出球上n+1个点,求出圆心. Solution 题中给出了对于n维空 ...
- Java线程之间通信
用多线程的目的:更好的利用CPU的资源.因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程. 并行:多个CPU实例或者多台机器同时执行一段处理逻辑, ...
- android HDMI 清晰度 分辨率
但改变分辨率时,发送广播即可: Intent intent_outputmode_change = new Intent(ACTION_OUTPUTMODE_CHANGE); intent_o ...
- 通过HTTP协议实现多线程下载
1. 基本原理,每条线程从文件不同的位置开始下载,最后合并出完整的数据. 2. 使用多线程下载的好处 下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...
- Spring学习4-面向切面(AOP)之aspectj注解方式
一.简介 1.AOP用在哪些方面:AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任,例如事务处理.日志管理.权限控制,异常处理等,封装起来,便于减少系统的重复代码,降低模块间的耦合 ...
- spark-submit提示资源不足
ensure that workers are registered and have sufficient resources spark-cluster启动的配置里配置了每个worker的内存,如 ...
- 一段代码了解Java中char和int的转换
题目要求: 将输入的大写字母转成对应小写的后5个,如A转换后为f:如果转换后大于z则从a重新计,即多出1就转成a,多出2就转成b以此类推. Java代码: ```java private static ...
- RSA算法小记
学习来源:http://www.cnblogs.com/vamei/p/3480994.html 小记: 一.数学基础: 欧拉Phi函数:Φ(n)=总数(从1到n-1中与n互质的整数) (1)欧拉定理 ...
- unity3d下载Obb分包文件
下载OBB插件包 http://pan.baidu.com/s/1c0ouRZE 1.导入插件 注意事项: 如果项目中已经存在Android 插件,需要merge导入的xml文件例如 AndroidM ...
- WEB移动应用框架构想(转载)
iUI.jQTouch.WPTouch.PhoneGap.XUI.iWebkit.Rhodes.gwt-mobile…当我们已经开始惊 叹 web移动应用充斥着各种各样框架与类库的时候,其实各大web ...