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
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry = 0; ListNode dummy = new ListNode(0); ListNode node = dummy; while(l1 != null || l2 != null) { int v1 = 0, v2 = 0; if(l1!=null) { v1 = l1.val; l1 = l1.next; } if(l2!=null) { v2 = l2.val; l2 = l2.next; } int sum = carry+v1+v2; carry = sum / 10; node.next = new ListNode(sum%10); node = node.next; } if(carry != 0) { node.next = new ListNode(carry); } return dummy.next; }
但是如果高位存在链表头部,低为存在链表尾部,该怎么办呢?
两种办法。
方法1:将两个链表反转,用上面方法加和,然后再把结果反转一下就可以了。
方法2:计算出两个链表的长度,假设链表l1的长度为len1,链表l2的长度为len2,且len1>=len2。递归的加低位的数字。
private int carry; public ListNode addNumber(ListNode l1, ListNode l2) { carry = 0; int len1 = length(l1), len2 = length(l2); ListNode node; if(len1 > len2) { node = addNumber(l1, l2, len1, len2); } else { node = addNumber(l2, l1, len2, len1); } if(carry == 0) return node; ListNode head = new ListNode(carry); head.next = node; return head; } private ListNode addNumber(ListNode l1, ListNode l2, int len1, int len2) { if(l1 == null) return null; ListNode next = addNumber(l1.next, len1 == len2 ? l2.next : l2, len1-1, len1 == len2 ? len2-1 : len2); int sum = l1.val+carry; if(len1==len2) { sum += l2.val; len2--; } len1--; carry = sum / 10; ListNode head = new ListNode(sum%10); head.next = next; return head; } private int length(ListNode head) { int len = 0; while(head != null) { len++; head = head.next; } return len; }
相关推荐
c c语言_leetcode 0002_add_two_numbers.zip
c语言入门 c语言_leetcode题解02-add-two-numbers.c
js js_leetcode题解之-add-two-numbers.js
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题解之002_Add_Two_Numbers
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 leetcode。 ...add-two-numbers leetcode --name median-of-two-sorted-arrays 然后问题描述和启动代码将自动
java基础 java_leetcode java题解之Add Two Numbers.java
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 Python 001 leetcode-问题-爬虫 ...002.add-two-numbers │ ├── information.json │ ├── README.md ... 有一些有用的选项: Options: -r, --rule crawling rule, eg1: 1-10, e
2. **Add Two Numbers**(两数相加):此题要求实现一个功能,将两个非负整数表示的链表相加。每个节点包含一个数字,链表的顺序代表数字的位值。解题方法通常是迭代或递归地处理链表,同时考虑进位。教程提供了C++...
Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output...
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 ...
leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。...add-two-numbers 3 Longest Substring Without Repeating Characters longest-substring-without-repeating-charac
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...
c++ C++_leetcode题解之002. Add Two Numbers.cpp
:[题目描述](https://leetcode-cn.com/problems/add-two-numbers),[解答](https://leetcode-cn.com/problems/add-two-numbers/solution/) 因为暂时只找到英文版的json数据,故现有格式为: - [ ] 0001.Two Sum:...
这个“python-leetcode面试题解之两数相加AddTwoNumbers.zip”压缩包聚焦于LeetCode中的一道经典面试题——"两数相加"(Add Two Numbers)。这道题主要考察的是链表操作和基本的计算逻辑。 题目描述:给定两个非空...
2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. ...
更新:添加README文件以及LeetCode前三题代码(Add README.md and Code for the first three questions) 1. 两数之和(Two Sum) 2. 两数相加(Add Two Numbers) 3. 无重复字符的最长子串(Longest Substring ...