博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
445. Add Two Numbers II - Medium
阅读量:6739 次
发布时间:2019-06-25

本文共 3056 字,大约阅读时间需要 10 分钟。

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

 

 

M1: 先把两个链表反转,相加之后再反转

time: O(n), space: O(n)

/** * 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) {        ListNode dummy = new ListNode(0);        ListNode prehead = dummy;        int remainder = 0;        ListNode p1 = reverse(l1), p2 = reverse(l2);        while(p1 != null || p2 != null) {            int sum = remainder;            if(p1 != null) {                sum += p1.val;                p1 = p1.next;            }            if(p2 != null) {                sum += p2.val;                p2 = p2.next;            }            dummy.next = new ListNode(sum % 10);            remainder = sum / 10;            dummy = dummy.next;        }        if(remainder != 0) {            dummy.next = new ListNode(remainder);        }        ListNode res = reverse(prehead.next);        return res;    }        private ListNode reverse(ListNode head) {        ListNode prev = null, cur = head;        while(cur != null) {            ListNode nextnode = cur.next;            cur.next = prev;            prev = cur;            cur = nextnode;        }        return prev;    }}

 

M2: follow-up 不能反转链表

用两个stack分别存储链表元素,再相加。

注意如果相加的话,得到的结果是反的,所以在相加的时候,就要边反转此结果链表

detail: 把dummy的值设为sum%10,tmp是新节点,其值为carry,tmp指向dummy,然后dummy向前移动一位,即dummy = tmp

最后还要判断一下leading number是不是0,如果为0,则返回下一个节点

time: O(n), space: O(n)

/** * 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) {        Stack
s1 = new Stack<>(); Stack
s2 = new Stack<>(); while(l1 != null) { s1.push(l1.val); l1 = l1.next; } while(l2 != null) { s2.push(l2.val); l2 = l2.next; } ListNode head = new ListNode(0); int carry = 0; while(!s1.isEmpty() || !s2.isEmpty()) { int sum = carry; if(!s1.isEmpty()) { sum += s1.pop(); } if(!s2.isEmpty()) { sum += s2.pop(); } head.val = sum % 10; ListNode newHead = new ListNode(sum / 10); newHead.next = head; head = newHead; carry = sum / 10; } return head.val != 0 ? head : head.next; }}

 

转载于:https://www.cnblogs.com/fatttcat/p/10122443.html

你可能感兴趣的文章
不要返回null之EmptyFactory
查看>>
Visual Studio 11 Beta新特性(一):安装VS11
查看>>
QQ-weiyun(微云)-云储存
查看>>
微信公众帐号开发教程第3篇-开发模式启用及接口配置(转)
查看>>
第 12 章 Other Web Server
查看>>
.NET项目web自动化测试实战——Selenium 2.0
查看>>
[LeetCode] Split Concatenated Strings 分割串联字符串
查看>>
Asp.Net SignalR - 持久连接类
查看>>
11.8. NAT
查看>>
PowerShell调用jira rest api实现jira统计自动化
查看>>
Git 时间,将代码托管到GitHub 上
查看>>
火车票秒杀攻略
查看>>
关于Asp.Net中FileUpload控件属性PostedFile.ContentType的提示
查看>>
Laravel5做权限管理
查看>>
Spring 通过Java代码装配bean
查看>>
架构重构-好文分享
查看>>
使用shell批量生成数据整合式迁移的脚本
查看>>
[20151021]理解dbms_xplan.display_cursor的format参数all.txt
查看>>
Unicode字符编码标准
查看>>
云计算就像是产业链的重新组合
查看>>