LeetCode2-两数相加

简介: LeetCode2-两数相加

题目


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


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


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

示例


输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)

输出:7 -> 0 -> 8

原因:342 + 465 = 807


代码



package com.leetcode.code;
import com.leetcode.source.ListNode;
/** 
* 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 
* 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 
* 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 
* 
* 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 
* 输出:7 -> 0 -> 8 
* 原因:342 + 465 = 807 
*/
public class LeetCode2 {    
  public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode head = new ListNode(0); 
    ListNode p = l1, q = l2, cur = head;
    int carry = 0; // 进位        
    while (p != null || q != null) {
      int x = (p != null) ? p.val : 0;            
      int y = (q != null) ? q.val : 0;            
      int sum = x + y + carry;           
      carry = sum / 10;            
      cur.next = new ListNode(sum % 10);            
      cur = cur.next;            
      if (p != null) {               
        p = p.next;            
      }           
      if (q != null) {                
        q = q.next;           
      }       
    }       
    if (carry > 0) {           
      cur.next = new ListNode(carry);       
     }        
     return head.next;    
  }
    public static ListNode buildListNode(int[] list) {  
      ListNode first = null, last = null, newNode;       
      for(int i = 0; i < list.length; i++) { 
        newNode = new ListNode(list[i]);            
        if(first == null) {                
          first = newNode;                
          last = newNode;            
        } else {                
            last.next = newNode;               
            last = newNode;            
          }        
        }        
        return first;   
    }
    private static void commonPrintListNode(ListNode listNode) {
      while(listNode!=null) {          
      String str = listNode.val + "->";        
      if (listNode.next == null) {           
          str = str.substring(0, str.length() - 2);      
         }          
         System.out.print(str);     
         listNode = listNode.next;      
       }   
    }
    public static void main(String[] args) {   
      int[] a = new int[]{2,4,3};     
      int[] b = new int[]{5,6,7};    
      ListNode aList = buildListNode(a);    
      ListNode bList = buildListNode(b); 
      ListNode listNode;
      listNode = aList;   
      commonPrintListNode(listNode);
      System.out.println();   
      listNode = bList;   
      commonPrintListNode(listNode);
      System.out.println();   
      listNode = addTwoNumbers(aList, bList);   
      commonPrintListNode(listNode);  
  }
}


输出结果:



2->4->3

5->6->4

7->0->8


小结



看完这个图,我相信大家都懂了。



这里需要注意一点就是这段代码:


if (carry > 0) {    
  cur.next = new ListNode(carry);
}

需要考虑末尾两个数相加超过10的话,那就当前cur的下一个值就是进位的值。


复杂度分析:

  • 时间复杂度:O(max(m, n)),假设 m 和 n 分别表示 l1 和 l2 的长度,上面的算法最多重复 max(m,n) 次。
  • 空间复杂度:O(max(m, n)),新列表的长度最多为 max(m, n) + 1。
相关文章
|
算法 测试技术 C++
C++算法:分发糖果
C++算法:分发糖果
|
C语言
【C】选择(一看就懂)——if语句和switch语句
【C】选择(一看就懂)——if语句和switch语句
259 0
【C】选择(一看就懂)——if语句和switch语句
|
人工智能 安全 数据可视化
阿里云创新生态合作手册-钉钉专场-上海铭悦:成就客户,成就自己,成就美好明天(中)
阿里云创新生态合作手册-钉钉专场-上海铭悦:成就客户,成就自己,成就美好明天(中)
382 0
|
资源调度 JavaScript 前端开发
【0成本搭建个人博客】——Hexo+Node.js+Gitee Pages
【0成本搭建个人博客】——Hexo+Node.js+Gitee Pages
312 0
|
测试技术
艾伟_转载:单件模式的陷阱
  看过很多单件模式的文章,书上有,网上更多一些。一般来说,只有如何实现单件模式,而没有介绍具体情况单件模式的使用,也没有介绍过单件模式会出现问题。单件模式似乎不会产生逻辑上的问题。但是,这仅仅是似乎。
928 0
|
1天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
11天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~

热门文章

最新文章