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
题目大意 ; 324 + 465 = 807 只是用链表表示 并且逆转了;
题目思路 :
链表的题目,必然又是许多细节性的问题 , 每个数的前一位 就是 链表中对应的下一个 , 所以只要遍历两个链表
逐位相加即可 ; 因为低位在前,所以不要担心 位数不同的情况;
但是要特别注意 这种情况[1] , [9 , 9 ,9] , 也就是他的进位操作(add1()方法)是递归的!!
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode l1It = l1; ListNode l2It = l2; ListNode head = l1; ListNode p = l1; if(l1 == null && l2 == null) return null; if(l1 == null) return l2; if(l2 == null) return l1; // 每个数的前一位 就是 链表中对应的下一个 while(l1It != null && l2It != null) { int tmp = l1It.val + l2It.val; p = l1; if(tmp < 10) { l1.val = tmp; l1 = l1.next; } else { l1.val = tmp - 10; l1 = l1.next; if(l1 != null) { add1(l1); } else if (l2It.next != null) { add1(l2It.next); } else { ListNode ln = new ListNode(1); p.next = ln; } } l1It = l1It.next; l2It = l2It.next; } if(l1It == null && l2It == null) return head; if(l1It == null) { p.next = l2It; return head; } else if(l2It == null) { p.next = l1It; return head; } return null; } public void add1(ListNode ln) { if(++ln.val >= 10) { ln.val = ln.val - 10; if(ln.next == null) { ListNode l = new ListNode(1); ln.next = l; } else { add1(ln.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版本
在上述示例中,`addtwonumber`方法计算两个整数的和并返回结果。 总结起来,Java编程中的控制语句、数组和类是构建复杂程序的基础。熟练掌握这些概念对于编写出高效、可维护的代码至关重要。通过不断的实践和学习,...
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 ...
AddTwoNumber 两数相加 完成 SwapPairs 两两交换链表中的节点 完成 String 题目 说明 状态 LongestSubstring 最长子串 完成 LongestPalindrome 最长回文子串 完成 Math 题目 说明 状态 ReverseInteger 翻转数字 完成...
AddTwoNumber:从数组中添加两个数字 BSTtoDLL:将二进制搜索树转换为双链表 BTreeIterator:二叉树迭代器:有序,前序,后序 BuildBT:从数组输入构建二进制 爬楼梯:爬楼梯问题 CombinationPermutation:组合和...
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 ...
例如,一个简单的函数`addTwoNumber`,接收两个整数参数并返回它们的和: ```go func addTwoNumber(a int, b int) int { return a + b } ``` 2. **参数类型与数量**: - Go语言支持无参数函数和有参数函数。 - ...
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 Two Numbers”是一个简单的JavaScript项目,涉及的基础知识点包括变量声明、数据类型(尤其是Number类型)、算术运算符(特别是加法运算符"+")、用户输入处理以及输出结果的方法。这些是JavaScript学习者初期...
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 decimal strings representing two integers Chapter 24. Generate all the bit patterns from 0 to such that successive patterns differ by one bit. Chapter 25. Represent unsigned integers with ...
# Definition for singly-linked list. ...#Use listnode to get a decimal number class GetNum: snode = None num = 0 #snode means the start node def __init__(self,snode:ListNode): self.snode=sno
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...
addTwoNumbers: addTwoNumbers, addArrayElements: addArrayElements }; })(); // 调用相应的函数 console.log(add.addTwoNumbers(1, 2)); // 输出:3 console.log(add.addArrayElements([1, 2, 3])); // 输出:...
There are two ways: clone this project, and use as dependency just add following code to you build.gradle: compile 'top.wuhaojie:scrollnumber:1.0.0' Usage Add this to your layout xml file: Call...
the number of selected items changed while the method was running. - ADD: Added the method TFlexControl.DoNeedHint - allows editing the displaying of hint for flex-object within the object. - ADD: ...
- (int)addTwoNumbers:(int)number1 secondNumber:(int)number2 { return number1 + number2; } int result = [self addTwoNumbers:5 secondNumber:3]; NSLog(@"%d", result); // 输出:8 ``` 【L005_ConnectPHP...
节点jsat 的J ava小号CRIPT甲nnotationŤransforms,或者,js- @ 安装 npm install --save-dev jsat ... addTwoNumbers ( firstNumber , secondNumber ) { return firstNumber + secondNumber ; }
a. Add two Rational numbers: The result of the addition should be stored in reduced form. b. Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. c. Multiply ...