You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. 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.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

可看成是Add Two NumbersReverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.

Time Complexity: O(n). reverse O(n), add O(n).

Space: O(1). reverse O(1), add O(1).

AC Java:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
}
l1 = reverseList(l1);
l2 = reverseList(l2); ListNode cur = l1;
int len1 = 0;
while(cur != null){
len1++;
cur = cur.next;
} cur = l2;
int len2 = 0;
while(cur != null){
len2++;
cur = cur.next;
} ListNode dummy = new ListNode(0);
if(len1 > len2){
dummy.next = l1;
}else{
dummy.next = l2;
}
cur = dummy;
int carry = 0; while(l1 != null || l2 != null){
if(l1 != null){
carry += l1.val;
l1 = l1.next;
}
if(l2 != null){
carry += l2.val;
l2 = l2.next;
}
cur.next.val = carry%10;
cur = cur.next;
carry /= 10;
}
if(carry != 0){
cur.next = new ListNode(1);
} ListNode nxt = dummy.next;
ListNode newHead = reverseList(nxt);
nxt.next = null;
return newHead;
}
private ListNode reverseList(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode tail = head;
ListNode cur = head;
ListNode pre;
ListNode temp;
while(tail.next != null){
pre = cur;
cur = tail.next;
temp = cur.next;
cur.next = pre;
tail.next = temp;
}
return cur;
}
}

LeetCode-Microsoft-Add Two Numbers II的更多相关文章

  1. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  2. LeetCode 445 Add Two Numbers II

    445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...

  3. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  4. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  5. LeetCode 445. Add Two Numbers II(链表求和)

    题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...

  6. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  7. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  10. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

随机推荐

  1. PHP函数总结 (三)

    <?php/** * PHP变量的范围 * 1.局部变量(内部变量) * 在函数内部声明的变量,作用域仅限于函数内部,参数也是局部变量:执行完毕后函数内部的变量都被释放 * 若需要使用函数内的变 ...

  2. python-day37--concurrent.futures模块 实现进程池与线程池

    1.concurrent.futures模块 直接内置就是 异步的提交   ,如果你想同步也可以实现(p.submit(task,i).result()即同步执行) 2.属性和方法: 1.submit ...

  3. HDU-4850 Wow! Such String! (构造)

    Problem Description Recently, doge starts to get interested in a strange problem: whether there exis ...

  4. 对LOV中的值进行强制验证

    当LOV之中只有一个LovMap返回当前ITEM时,修改了LOV输入框的值,会弹出验证窗口,若此时忽略此窗口,在进行下一步的时候不会去验证此LOV中的值是否一定在可选列表中. 解决方式, 1.在页面加 ...

  5. linux page table entry struct

    Page Table Entry The access control information is held in the PTE and is CPU specific; figure bit f ...

  6. anroid学习目录总结

    当前标签: Android开发学习总结   Android开发学习总结(六)—— APK反编译 孤傲苍狼 2015-07-26 12:48 阅读:4245 评论:5     Android开发学习总结 ...

  7. How to pass string parameters to an TADOQuery?

    http://4byte.cn/question/1130217/how-to-pass-string-parameters-to-an-tadoquery.html 从2个答案看,如果TADOQue ...

  8. NETGEAR WNDR3800CH openwrt 不能用新版, Barrier Breaker 14.07

    15系列主要是不能正常端口映射,这个很不方便了. 尽管80端口被封了,我们可以用别的端口啊. 刷完以后,不懂英文的,跟着下面的步骤就可以进入中文环境了. 记得先上网,再通过路由下载安装中文包,才可以. ...

  9. Pycharm(一)下载安装

    https://www.python.org/downloads/windows/ 这里下载python,建议2.7,3.6都下载 Download Windows x86 web-based ins ...

  10. PHP生成GIF动态图片验证码

    <?php /** * 调用示例 * */ session_start(); $randCode = ''; //验证码随机 $str="abcdefghjkmnpqrstuvwsyz ...