leetcode 002 两数相加 Java题解

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

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

示例 1:

leetcode 002 两数相加 Java题解

 

 

 

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.

提示:

  • 每个链表中的节点数在范围 [1, 100] 内
  • 0 <= Node.val <= 9
  • 题目数据保证列表表示的数字不含前导零

 

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 class Solution {
12     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
13         ListNode head1 = l1;
14         ListNode head2 = l2;
15         ListNode head = new ListNode(-1);
16         ListNode memo = head;
17         int last = 0;
18         while(head1!=null&&head2!=null){
19             int add = head1.val + head2.val + last;
20             if(add<10){
21                 ListNode temp = new ListNode(add);
22                 head.next = temp;
23                 head = head.next;
24                 last = 0;
25             }else{
26                 int plus = add%10;
27                 ListNode temp = new ListNode(plus);
28                 head.next = temp;
29                 head = head.next;
30                 last = add/10;
31             }
32             head1 = head1.next;
33             head2 = head2.next;
34         }
35 
36         if(head1==null){
37             //遍历head2
38             while(head2!=null){
39                 int add = head2.val + last;
40                 if(add<10){
41                 ListNode temp = new ListNode(add);
42                 head.next = temp;
43                 head = head.next;
44                 last = 0;
45                 }else{
46                 int plus = add%10;
47                 ListNode temp = new ListNode(plus);
48                 head.next = temp;
49                 head = head.next;
50                 last = add/10;
51                 }
52                 head2 = head2.next;
53             }
54 
55             if(last!=0){
56                 while(last!=0){
57                     int plus = last%10;
58                     ListNode temp = new ListNode(plus);
59                     head.next = temp;
60                     head = head.next;
61                     last = last/10;
62                 }
63             }
64         }
65             
66         if(head2==null){
67             while(head1!=null){
68                 int add = head1.val + last;
69                 if(add<10){
70                 ListNode temp = new ListNode(add);
71                 head.next = temp;
72                 head = head.next;
73                 last = 0;
74                 }else{
75                 int plus = add%10;
76                 ListNode temp = new ListNode(plus);
77                 head.next = temp;
78                 head = head.next;
79                 last = add/10;
80                 }
81                 head1 = head1.next;
82             }
83 
84             if(last!=0){
85                 while(last!=0){
86                     int plus = last%10;
87                     ListNode temp = new ListNode(plus);
88                     head.next = temp;
89                     head = head.next;
90                     last = last/10;
91                 }
92             }
93         }
94             
95         return memo.next;
96     }
97 }

很简单的思路,超过了百分之99%

 

思路:列表按序相加,并加上进位就行了

 

leetcode 002 两数相加 Java题解

 

 

 

leetcode 002 两数相加 Java题解

上一篇:002 c++中的输入输出的概念


下一篇:嵌入式100题(002):多进程、多线程的优缺点