【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 解题思路:将数字 ...
随机推荐
- PLSQL导入Excel数据方法
1.把Excel文件另存为(文本文件(制表符分隔)(*.txt)) 2.把新生成的student.txt文件导入到plsql 打开plsql连接到要导入的oracle数据库再打开Tools - ...
- 找回Reshaprer的Alt+Enter快捷键的方法
用过Reshaprer一段时间发现这个Visual Studio插件确实是个好东东,特别是神级快捷键Alt+Enter更是好用至极,可以解决大部分代码问题,不过会发现装上Reshaprer后VS自带的 ...
- 利用symbolsource/gitlink调试你的nuget包
关键字: 如何调试Nuget下载的dll? VS github 调试 参考文章: http://docs.nuget.org/create/creating-and-publishing-a-sy ...
- CODEVS1995 || TYVJ1863 黑魔法师之门
P1863 [Poetize I]黑魔法师之门 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源 ...
- Tomcat Server Configuration Automation Reinforcement
目录 . 引言 . 黑客针对WEB Server会有那些攻击面 . 针对Tomcat Server可以做的安全加固 . Managing Security Realms with JMX . 实现对T ...
- 怎样分析java线程堆栈日志
注: 该文章的原文是由 Tae Jin Gu 编写,原文地址为 How to Analyze Java Thread Dumps 当有障碍,或者是一个基于 JAVA 的 WEB 应用运行的比预期慢的时 ...
- mysql 用户方面的操作
1.只新建用户的操作 mysql -u root -p密码mysql> insert into mysql.user(Host,User,Password) values(‘localhost’ ...
- C语言计算任意数的任意次方
#include "stdio.h" #include"stdlib.h" #define max 500 void yiwei(int *a,int n,in ...
- 使用SqlSessionTemplate实现数据库的操作
EmployeeMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE map ...
- 解决Jquery对input file控件的onchange事件只生效一次的问题
如题,解决办法的代码如下: 1. $('#fileId').live('change',function(){ //逻辑添加.... }); 2. $('#fileId').change(functi ...