又是一篇漏网之鱼,请大家移步新博文地址:[leetcode]Add Two Numbers
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
算法思路:
思路1:
将两个因数转换成整型,求出和,再将和转换为链表形式
【注意】这个int类型会溢出,严格来说,long也会溢出,因此是不严谨的。
代码如下:
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; return num2List( getNum(l1) + getNum(l2)); } private long getNum(ListNode l){ if(l == null) return 0; long num = 0; long length = 0; while( l != null){ num += Math.pow(10,length) * l.val; length++; l = l.next; } return num; } private ListNode num2List(long num){ ListNode hhead = new ListNode(0); ListNode tail = hhead; if(num == 0) return hhead; while(num != 0){ ListNode tem = new ListNode((int)(num % 10)); tail.next = tem; tail = tail.next; num /= 10; } return hhead.next; } }
思路2:
直接在两个list中进行加和操作。模拟加法。
1 public class Solution { 2 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 3 if(l1 == null) return l2; 4 if(l2 == null) return l1; 5 int l1Length = getLength(l1),l2Length = getLength(l2); 6 if(l1Length > l2Length) return addTwoNumbers(l2, l1); 7 ListNode l2Pointer = l2; 8 ListNode l1Pointer = l1; 9 while(l2Pointer != null){ 10 l2Pointer.val = l2Pointer.val + ((l1Pointer == null) ? 0 : l1Pointer.val); 11 if(l2Pointer.val > 9){ 12 l2Pointer.val -= 10 ; 13 if(l2Pointer.next == null){ 14 ListNode tail = new ListNode(1); 15 l2Pointer.next = tail; 16 break; 17 } 18 l2Pointer.next.val++; 19 } 20 l2Pointer = l2Pointer.next; 21 if(l1Pointer != null)l1Pointer = l1Pointer.next; 22 } 23 return l2; 24 } 25 private int getLength(ListNode list){ 26 int length = 0; 27 while(list != null){ 28 length++; 29 list = list.next; 30 } 31 return length; 32 } 33 }
相关推荐
自己写的一个完整的程序,包括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版本
leetcode:Add Two Numbers(java)
python python_leetcode面试题解之两数相加AddTwoNumbers
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
手绘算法力扣 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
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 ...
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...
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 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...
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], 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. ...
leetcode 答案leetcode javascript 中 leetcode 测试的答案 twoSum addTwoNumbers
Daily-LeetCode02AddTwoNumbers- 5.spark包 详情参见 6.unsafe包 闲暇时看一些Java关于unsafe的文章时写的一些代码 7.algorithm包-算法 自己的一些白话理解 1.选择排序,时间复杂度是O(n^2),空间复杂度是O(1),不稳定...
leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。...add-two-numbers 3 Longest Substring Without Repeating Characters longest-substring-without-repeating-charac
Numbers JavaScript O(n) O(1) Medium 4 Median of Two Sorted Arrays JavaScript O(log (m+n)) O(1) Hard 7 Reverse Integer JavaScript O(n) O(1) Easy 9 Palindrome Number JavaScript O(n) O(1) Easy 19 Remove ...
leetcode 2 和 c #Chao Xu 的 LeetCode 笔记 # 标题 困难 分析 代码 1 二和 中等的 [C++] (./C++/Two Sum.cpp), [Java] (./Java/Two Sum.java) 2 两个数相加 中等的 [C++] (./C++/Add Two Numbers.cpp), [Java] (./...