Add Two Numbers
链表相加
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode result = new ListNode(0);
ListNode start = result;
int temp = 0;
ListNode t1 = l1;
ListNode t2 = l2;
int t1v;
int t2v;
for (; t1 != null || t2 != null;) {
result.next = new ListNode(0);
result = result.next;
if (t1 == null)
t1v = 0;
else {
t1v = t1.val;
t1 = t1.next;
}
if (t2 == null)
t2v = 0;
else {
t2v = t2.val;
t2 = t2.next;
}
result.val = (t1v + t2v + temp) % 10;
temp = (t1v + t2v + temp) / 10;
}
if (temp != 0) {
result.next = new ListNode(0);
result.next.val = temp;
}
return start.next;
}
分享到:
相关推荐
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版本
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
python python_leetcode面试题解之两数相加AddTwoNumbers
leetcode:Add Two Numbers(java)
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
手绘算法力扣 2 两数相加(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
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
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 ...
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where...
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 ...
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/...
leetcode 2 和 c 加二数解 LeetCode 两个数字相加的 C 语言解决方案 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of ...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
leetcode 答案leetcode javascript 中 leetcode 测试的答案 twoSum addTwoNumbers
leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。 详情 # Title Solution Time Space Difficulty 1 Two ...
leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。...add-two-numbers 3 Longest Substring Without Repeating Characters longest-substring-without-repeating-charac
Daily-LeetCode02AddTwoNumbers- 5.spark包 详情参见 6.unsafe包 闲暇时看一些Java关于unsafe的文章时写的一些代码 7.algorithm包-算法 自己的一些白话理解 1.选择排序,时间复杂度是O(n^2),空间复杂度是O(1),不稳定...
leetcode第321题 说明 每个代码里面都包含如下几部分: 自动生成测试数据的函数: random() 测试程序: main() 核心函数: 和LeetCode上面的模板函数同名 其他: 功能参见注解说明 注意:为了可以在测试程序里调用...