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. DownLoadImage

    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA&quo ...

  2. (GoRails )使用Vue.js制作拖拉list功能(v5-8)

    视频5 改进视觉效果,让list看起来更舒服.新增横向滚动功能. 参考我的trello:https://trello.com/b/BYvCBpyZ/%E6%AF%8F%E6%97%A5%E8%AE%B ...

  3. android--------根据文件路径加载指定文件

    Android根据指定的文件路径,加载该路径下指定文件格式(图片格式 png, gif,jpg jpeg)的文件相关信息的列表. 如图: public class MainActivity exten ...

  4. Dubbo 环境搭建

    CentOS7 x64 JDK1.8 1. wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.8/zoo ...

  5. python-day21--time模块

    一.三种表示方法 1.时间戳(timestamp): time.time( )       #得到的是float类型 2.格式化(Format String): time.strftime('%Y/% ...

  6. python-day14--带参数的装饰器+多个装饰器装饰同一个函数

    1.# 带参数的装饰器def f1(flag): def f2(func): def inner(*args,**kwargs): if flag: '''执行函数之前要做的''' r=func(*a ...

  7. EBS OAF开发中的错误/异常处理(ErrorHandling) (转)

    原文地址 EBS OAF开发中的错误/异常处理(ErrorHandling) EBS OAF开发中的错误/异常处理(ErrorHandling) (版权声明,本人原创或者翻译的文章如需转载,如转载用于 ...

  8. linux磁盘清理记录

    执行df -h查看自己的数据磁盘到达97% 使用du -h --max-depth=1  查看数据占用较大的文件 清理记录 1.mysql慢查询日志 # mv slow.log slow.log.ba ...

  9. Tomcat类加载器破坏双亲委派

    转载:https://blog.csdn.net/qq_38182963/article/details/78660779 http://www.cnblogs.com/aspirant/p/8991 ...

  10. React-Router v4.0 hashRouter使用js跳转

    React-Router v4.0上已经不推荐使用hashRouter,主推browserRouter,但是因为使用browserRouter需要服务端配合可能造成不便,有时还是需要用到hashRou ...