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版本
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 ...
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 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...
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: ...
p1.add(Jnumber);p2=new JPanel();p2.add(new JLabel("姓名:",JLabel.CENTER));p2.add(Jname);p3=new JPanel();p3.add(new JLabel("性别:",JLabel.CENTER));p3.add(boy);p3.add(girl);p4=new JPanel();p4.add(new ...
节点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 ...
Email address book includes two types of email address: u General address, include information: email address (unique), name, phone number u Group address, include information: email address (unique)...
Badge Preview Using with gradle Add the JitPack repository to your root ...TYPE_NUMBER TYPE_ONLY_ONE_TEXT TYPE_TWO_TEXT TYPE_TWO_TEXT_COMPLEMENTARY Attributes Usage The above screenshot'
This paper describes the connection ... Our investigations on two typical network topologies show that the number of add/drop ports of OXCs has a significant impact on the LSP blocking performance.
// Addition program that displays the sum of two integers. #include <iostream> // allows program to perform input and output // function main begins program execution int main() { // variable ...
memorize BASH (unix shell add a c in the middle) EMPU (electromagnetic pulse) and 1720 B - B Channel MGCP / BRQ for Gatekeeper A - AAR S - Stop Routing H323 (same as H) C - Codecs disable both 722 ...