【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 解题思路:将数字 ...
随机推荐
- C语言中常用的string.h的字符函数
strcmp 字符串比较函数 原型: int strcmp(char *str1, char *str2); 例子: ) printf("buffer 1 is greater than b ...
- 【bzoj3246】 Ioi2013—Dreaming
www.lydsy.com/JudgeOnline/problem.php?id=3246 (题目链接) 题意 给出一棵不完全的树,要求在树上连最少的边使得所有点联通,并且使得两点间最大距离最小. S ...
- JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
建立了一个Javaweb工程,并在eclipse中配置了Web容器Tomcat.新建的jsp页面,添加一个简单的Java类.可是,JSP页面顶端出现“红色”的报错信息:The superclass & ...
- C#文件复制功能
目的是将用户自定义文件复制到指定文件夹并且能查看该文件,下面是个人做的源码: sing System; using System.Collections.Generic; using System.C ...
- Codeforces 556D Restructuring Company
传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...
- OOA/OOD/OOP(了解)
Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构化分析有较大的区别.OOA所强调的是在系统 ...
- 使用migrate.exe执行EF code first 迁移
Code First 迁移可用于从 Visual Studio 内部更新数据库,但也可通过命令行工具 migrate.exe 来执行.本页简单介绍如何使用 migrate.exe 对数据库执行迁移. ...
- CSS 2D转换 matrix() 详解
2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...
- windows2003安全加固
因为IIS的方便性和易用性,使它成为最受欢迎的Web服务器软件之一.但是,IIS的安全性却一直令人担忧.如何利用IIS建立一个安全的Web服务器,是很多人关心的话题.要创建一个安全可靠的Web服务器, ...
- EL表达式从request和session中取值
在Action中保存登录的基本信息:request.getSession().setAttribute("adminid", str); 在JSP页面中:${sessionScop ...