两数相加(非空链表)的两种题型


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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
示例:
输入:
(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807
 
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10     public ListNode addTwoNumbers(ListNode a, ListNode b) {
11                 ListNode res=null;
12                 ListNode head=null;
13                 int over=0;
14                 int length=0;
15                 while (true){
16                     int value=(a.val+b.val)%10+over;
17                     if(value>9){
18                         value=0;
19                         over=1;
20                     }else{
21                         over=(a.val+b.val)/10;
22                     }
23                    ListNode temp=new ListNode(value);
24                    // System.out.println(temp.val);
25                     if (length==0){
26                         res=temp;
27                         head=res;
28                     }else{
29                         res.next=temp;
30                         res=res.next;
31                     }
32                    // res=res.next;
33                     a=a.next;
34                     b=b.next;
35                     if (a == null && b != null) {
36                         a=new ListNode(0);
37                     }
38                     if( a!=null && b==null){
39                         b=new ListNode(0);
40                     }
41                     if (a==null&&b==null){
42                         if(over==0){
43                          break;
44                         }else{
45                             res.next=new ListNode(1);
46                             break;
47                         }
48                     }
49                 
50                     length++;
51                 }       
52                 return head;
53     }
54 }

第二种题型:

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

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

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。


示例:

输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 8 -> 0 -> 7



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

* Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode a, ListNode b) {
/*原本准备逃个巧结果被一个比较过分的测试案例卡住了,导致这种方法无法通过*/
    ListNode temp=a;
                int av=0;
                while (temp!=null){
                    av=10*av+temp.val;
                    temp=temp.next;
                }
                // System.out.println(av);
       // System.out.println("a->"+a);
                int bv=0;
                temp=b;
                while (temp!=null){
                    bv=10*bv+temp.val;
                    temp=temp.next;
                }
                //System.out.println(bv);
                int sum=av+bv;
                ArrayList listNodes=new ArrayList<>();
                if(sum==0){
                    return new ListNode(0);
                }
                while (sum!=0){
                   // if ()
                    ListNode tempNode=new ListNode(sum%10);
                    listNodes.add(tempNode);
                    //res.next=tempNode;
                    //System.out.println(res.val);
                   //ListNode res=new ListNode(sum%10);
                    sum=sum/10;
                }
                for (int i=listNodes.size()-1;i>0;i--){
                    listNodes.get(i).next=listNodes.get(i-1);
                }            
                return listNodes.get(listNodes.size()-1);
    
    }
}

题解中的一个方法

 https://leetcode-cn.com/problems/add-two-numbers-ii/solution/javakai-fa-by-sweetiee/