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
这题比较简单,就是一个大数据的加法。JAVA中有专门的一个类实现。BigInteger BigDecimal的源代码可以参考。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ln = new ListNode(0);
int num = 0;
ListNode end = ln;
if(l1 == null&&l2!= null){
ln = l2;
}else if(l2 == null && l1 != null){
ln = l1;
}else if(l1 != null && l2 != null){
ln.val = (l1.val+l2.val+num)%10;
num = (l1.val+l2.val+num)/10;
while(l1.next != null && l2.next != null){
l1 = l1.next;
l2 = l2.next;
ln.next = new ListNode(0);
ln = ln.next;
ln.val = (l1.val+l2.val+num)%10;
num = (l1.val+l2.val+num)/10;
}
if(l1.next == null && l2.next != null){
while (l2.next!= null){
ln.next = l2.next;
l2 = l2.next;
ln = ln.next;
int val = l2.val;
ln.val = (val+num)%10;
num = (val+num)/10;
}
}else
if(l2.next == null && l1.next != null){
while (l1.next!= null){
ln.next = l1.next;
l1 = l1.next;
ln = ln.next;
int val = l1.val;
ln.val = (val+num)%10;
num = (val+num)/10;
}
}
if(l1.next == null && l2.next == null){
if(num == 1){
ln.next = new ListNode(1);
}
}
}
return end;
}
}
指针的运用。
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
这题比较简单,就是一个大数据的加法。JAVA中有专门的一个类实现。BigInteger BigDecimal的源代码可以参考。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ln = new ListNode(0);
int num = 0;
ListNode end = ln;
if(l1 == null&&l2!= null){
ln = l2;
}else if(l2 == null && l1 != null){
ln = l1;
}else if(l1 != null && l2 != null){
ln.val = (l1.val+l2.val+num)%10;
num = (l1.val+l2.val+num)/10;
while(l1.next != null && l2.next != null){
l1 = l1.next;
l2 = l2.next;
ln.next = new ListNode(0);
ln = ln.next;
ln.val = (l1.val+l2.val+num)%10;
num = (l1.val+l2.val+num)/10;
}
if(l1.next == null && l2.next != null){
while (l2.next!= null){
ln.next = l2.next;
l2 = l2.next;
ln = ln.next;
int val = l2.val;
ln.val = (val+num)%10;
num = (val+num)/10;
}
}else
if(l2.next == null && l1.next != null){
while (l1.next!= null){
ln.next = l1.next;
l1 = l1.next;
ln = ln.next;
int val = l1.val;
ln.val = (val+num)%10;
num = (val+num)/10;
}
}
if(l1.next == null && l2.next == null){
if(num == 1){
ln.next = new ListNode(1);
}
}
}
return end;
}
}
指针的运用。
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 325Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 363Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 307Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 339Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 330Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 311Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 291Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 317Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 327Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 326Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 289Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 321Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 366Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 330Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 340Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 227Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 254The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 333Given a string S, find the long ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 425[size=24px;]Longest Substring W ...
相关推荐
You are given two non-empty linked lists ... Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. java AC版本
leetcode:Add Two Numbers(java)
手绘算法力扣 2 两数相加(Add Two Numbers)
python python_leetcode面试题解之两数相加AddTwoNumbers
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;...
Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
js代码-2. Add Two Numbers
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
leetcode 2 和 c 2021-LeetCode-02_Add_...addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { guard l1 != nil && l2 != nil else { return nil } var resultTail = ListNode() let resultHead = resu
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None #Use listnode to get a decimal number class GetNum: snode = None num = 0 ...
Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum ...
Add two numbers without using arithmetic operators Chapter 10. Given an array of integers where all the numbers are appearing twice find the only number which appears once Chapter 11. Given an array ...
挑战:01-addOne 难度:基本Swift的: 编写一个名... 例子: addTwoNumbers(5,10)-> 15 addTwoNumbers(10,-2)-> 8 addTwoNumbers(0,0)-> 0 addTwoNumbers('Hello',5)-> NaN function addTwoNumbers ( num1
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
add two numbers in java
Java Program To Add Two Numbers
节点jsat ... * @deprecated addTwoNumbers() will be removed in v2.0.0; please use add() */ exports . addTwoNumbers ( firstNumber , secondNumber ) { return firstNumber + secondNumber ; }