• leetcode-2


    给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

    如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

    您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

    示例:

    输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
    输出:7 -> 0 -> 8
    原因:342 + 465 = 807

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/add-two-numbers
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) p = p.next;
            if (q != null) q = q.next;
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return dummyHead.next;
    }

    时间复杂度是max(m, 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) {
            int l1val = 0;
            int l2val = 0;
    
            
            if (l1==null){l1val =0;}
            else {l1val = l1.val;}
            
            if (l2==null){l2val =0;}
            else {l2val = l2.val;}
    
            if ((l1val + l2val)>=10) {
    
                if (l1.next !=null ){l1.next.val++;}
                else if (l2.next !=null){l2.next.val++;}
            }
            
            ListNode rtnNode = new ListNode( (l1val + l2val)%10);
    
            if (l1.next==null && l2.next==null ){
                if ((l1val + l2val)>=10){
                    rtnNode.next = new ListNode(1);
                }
                else {rtnNode.next =null;}
                }
            else if (l1.next ==null && l2.next !=null){
                rtnNode.next =  addTwoNumbers(new ListNode(0), l2.next);
            }
            else if (l1.next != null && l2.next ==null){
                rtnNode.next =  addTwoNumbers(l1.next, new ListNode(0));
            }
    
            else {rtnNode.next = addTwoNumbers(l1.next, l2.next);}
         
               return rtnNode; 
    
            
        }
    }
    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    Tomcat9报错 The valid characters are defined in RFC 7230 and RFC 3986
    Job for ssh.service failed because the control process exited with error code.......
    解决 ora-28001 密码过期的处理办法
    Vmware unknow Interface ens33
    ubuntu桌面环境安装中文环境
    (CRON) info (No MTA installed, discarding output)” error in the syslog
    SSH Secure Shell链接Ubuntu报错Server responded "Algorithm negotiation failed"
    mysql备份数据库出错mysqldump: [ERROR] unknown option '--no-beep'
    dclcommon200.bpl
    字节 汉字判断
  • 原文地址:https://www.cnblogs.com/CherryTab/p/12015836.html
Copyright © 2020-2023  润新知