/**
*
* <pre>
* 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
* </pre>
*/
public class AddTwoNumbers {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
int value = (l1.val + l2.val) % 10;
int carry = (l1.val + l2.val) / 10;
ListNode head = new ListNode(value);
ListNode tail = head;
l1 = l1.next;
l2 = l2.next;
while (!(l1 == null && l2 == null && carry == 0)) {
int temValue = carry;
if (l1 != null) {
temValue += l1.val;
}
if (l2 != null) {
temValue += l2.val;
}
value = (temValue) % 10;
carry = (temValue) / 10;
ListNode node = new ListNode(value);
tail.next = node;
tail = node;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
return head;
}
}
}
分享到:
相关推荐
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
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版本
java基础 java_leetcode 题解之 Add Two Numbers II.java
java基础 java_leetcode java题解之Add Two Numbers.java
c++ C++_leetcode题解之002. Add Two Numbers.cpp
leetcode:Add Two Numbers(java)
这个“python-leetcode面试题解之两数相加AddTwoNumbers.zip”压缩包聚焦于LeetCode中的一道经典面试题——"两数相加"(Add Two Numbers)。这道题主要考察的是链表操作和基本的计算逻辑。 题目描述:给定两个非空...
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
手绘算法力扣 2 两数相加(Add Two Numbers)
c c语言_leetcode 0002_add_two_numbers.zip
c语言入门 c语言_leetcode题解02-add-two-numbers.c
js js_leetcode题解之-add-two-numbers.js
java入门 java_leetcode题解之002_Add_Two_Numbers
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
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 ...
2. **Add Two Numbers (两数相加)**: 该问题是关于链表操作的,要求将两个非负整数表示为链表形式,然后将它们相加。这需要理解链表的结构,如节点、头结点、指针等,以及如何在链表上进行加法运算。C++中,我们可以...
* 添加两个数字(Add Two Numbers):将两个数字相加,并将结果以链表的形式返回。 * 反向整数(Reverse Integer):将整数反转。 * 字符串到整数(atoi)(String to Integer (atoi)):将字符串转换为整数。 2....
2. **Add Two Numbers** (Medium) 此题要求将两个链表表示的非负整数相加,结果仍以链表形式返回。每个节点代表一个数字位。从最低位开始逐位相加,处理进位,并构建新的链表。要注意链表可能有不同的长度,处理零的...
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/...