原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/

题目:

You are given two linked lists representing two non-negative numbers. 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 Numbers.

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

Time Complexity: O(n).

Space: O(1).

AC Java:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverse(l1);
l2 = reverse(l2); ListNode dummy = new ListNode(0);
ListNode 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 = new ListNode(carry%10);
carry /= 10;
cur = cur.next;
} if(carry != 0){
cur.next = new ListNode(carry);
} ListNode head = dummy.next;
dummy.next = null;
return reverse(head);
} private ListNode reverse(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;
}
}

也可以使用两个stack把list 1 和 list 2 分别压进去. 再pop出来相加放到new list的head位置.

Time Complexity: O(n). 压stack用了O(n), pop后相加用了O(n).

Space: O(n). stack用了O(n). result list用了O(n).

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;
} Stack<Integer> stk1 = new Stack<Integer>();
Stack<Integer> stk2 = new Stack<Integer>();
while(l1 != null){
stk1.push(l1.val);
l1 = l1.next;
}
while(l2 != null){
stk2.push(l2.val);
l2 = l2.next;
} ListNode dummy = new ListNode(0);
int carry = 0;
while(!stk1.isEmpty() || !stk2.isEmpty()){
if(!stk1.isEmpty()){
carry += stk1.pop();
}
if(!stk2.isEmpty()){
carry += stk2.pop();
}
ListNode cur = new ListNode(carry%10);
cur.next = dummy.next;
dummy.next = cur;
carry /= 10;
}
if(carry != 0){
ListNode cur = new ListNode(1);
cur.next = dummy.next;
dummy.next = cur;
}
return dummy.next;
}
}

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

  1. [LeetCode] 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 两个数字相加之二

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

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

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

  4. 445. Add Two Numbers II - LeetCode

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

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

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. LeetCode 445 Add Two Numbers II

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

  7. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  8. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  9. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

随机推荐

  1. vim使用技巧

    1.vim 编辑下内容复制.剪切到windows下 "+y 或 "+d 2.从win 复制到 vim CTRL+Shift+v 3.选择文字 v+方向键

  2. Win10 UI介绍之Titlebar

    活动状态 非活动状态 var titleBar = ApplicationView.GetForCurrentView().TitleBar; titleBar.BackgroundColor = C ...

  3. C#将数据大小字节转换为MB,GB,TB

    http://www.myluoluo.com/c%E5%B0%86%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%E5%AD%97%E8%8A%82%E8%BD%AC%E6 ...

  4. C# 读本地INI文件方法

    [DllImport("kernel32")]//加载dll private static extern int GetPrivateProfileString(string se ...

  5. Subsonic简单的语法整理

    .查询方面 (查询所有数据记录[dataset读取方法]) Myuser.Query().ExecuteDataSet().Tables[]; Myuser.Query().ExecuteDataSe ...

  6. 【Cocos2d-x游戏开发】Cocos2d-x中的数据存储技术

    一.引言 数据存储和网络功能可以说是一款游戏中必不可少的功能,如果一款游戏不能保存进度那么它的可玩性必然大打折扣(试想一下,玩家辛辛苦苦玩了一整天的游戏,结果退出时告诉人家不能保存关卡信息,你明天还得 ...

  7. Python中输出格式化的字符串

    在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下: >>> 'Hello, %s' % 'world' 'Hello, world' >>> ...

  8. 进制转换( C++字符数组 )

    注: 较为简便的方法是用 整型(int)或浮点型(long.double 注意:该类型不一定能够准确存储数据) 来存放待转换的数值,可直接取余得到每一位数值 较为稳定的方法是用 字符数组储存待转换的数 ...

  9. <三>JDBC_面向对象思想的体现

    JDBCTools.java import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;i ...

  10. jquery.cookie.js && java后台代码 操作cookie实现记住当前用户输入信息代码

    下载jquery.cookie.js地址看这里:http://pan.baidu.com/s/1gdCPaN5 //初始化页面时验证是否记住了密码 $(document).ready(function ...