两数求和-链表

时间:2019-08-24
本文章向大家介绍两数求和-链表,主要包括两数求和-链表使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目:

第一次提交:失败

 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 l1, ListNode l2) {
11         
12         ListNode l3 = null,p = null,q=null;
13         int temp = 0;
14         int sup =0;
15         int count = 0;
16         while(l1!=null&&l2!=null){
17             temp = l1.val+l2.val+sup;
18             sup = 0;
19             if(temp>=10){
20                 sup = temp/10;
21                 temp = temp%10;
22                
23             }    
24             p = new ListNode(temp);
25             p.next = null;
26             if(count==0){
27                   l3 = p;
28                     q=p;
29                  count++;
30             }else{
31                 q.next = p;
32                 q = p;
33             } 
34             l1 = l1.next;
35             l2 = l2.next;
36         }
37         if(l1==null&&l2!=null){
38             //todo if l2.val >10  loop
39             while(l2!=null){
40                temp = l2.val+sup;
41                   if(temp>=10){
42                         sup = temp%10;
43                         temp = temp/10;
44                    } 
45                  l2.val = temp;
46                 l2=l2.next;
47             }
48             q=l2;
49         }else if(l1!=null&&l2==null){
50             while(l1!=null){
51                temp = l1.val+sup;
52                   if(temp>=10){
53                         sup = temp%10;
54                         temp = temp/10;
55                    } 
56                  l1.val = temp;
57                 l1=l1.next;
58             }
59             q=l1;
60         }else{
61             // do nothing
62         }
63         return l3;
64     }
65 }

错误,当用例:【5】、【5】 我的输出:【0】,实际:【0,1】

 第二次提交:通过

 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 l1, ListNode l2) {
11         
12         ListNode l3 = null,p = null,q=null,l22=null,l11=null;
13         int temp = 0;
14         int sup =0;
15         int count = 0;
16         while(l1!=null&&l2!=null){
17             temp = l1.val+l2.val+sup;
18             sup = 0;
19             if(temp>=10){
20                 sup = temp/10;
21                 temp = temp%10;
22                
23             }    
24             p = new ListNode(temp);
25             p.next = null;
26             if(count==0){
27                   l3 = p;
28                     q=p;
29                  count++;
30             }else{
31                 q.next = p;
32                 q = p;
33             } 
34             l1 = l1.next;
35             l2 = l2.next;
36         }
37         if(l1==null&&l2!=null){
38             //todo if l2.val >10  loop
39             l22 = l2;
40             while(l2!=null){
41                temp = l2.val+sup;
42                 sup = 0;
43                   if(temp>=10){
44                        sup = temp/10;
45                        temp = temp%10;
46                    } 
47                 l2.val = temp;
48                 if(sup!=0&&l2.next==null){
49                     p = new ListNode(sup);
50                     p.next = null;
51                     l2.next=p;
52                     break;
53                 }
54                 
55                 l2=l2.next;
56             }
57             q.next=l22;
58         }else if(l1!=null&&l2==null){
59             l11 = l1;
60             while(l1!=null){
61                temp = l1.val+sup;
62                 sup =0;
63                   if(temp>=10){
64                         sup = temp/10;
65                         temp = temp%10;
66                    } 
67                 l1.val = temp;
68                 if(sup!=0&&l1.next==null){
69                     p = new ListNode(sup);
70                     p.next = null;
71                     l1.next=p;
72                      break;
73                 }
74                 l1=l1.next;
75             }
76             q.next=l11;
77         }else{
78             if(sup!=0){
79                 p = new ListNode(sup);
80                 p.next = null;
81                 q.next = p;
82             }
83             // do nothing
84         }
85         return l3;
86     }
87 }

问题:代码重复性太高,变量多而复杂

 第三次提交

原文地址:https://www.cnblogs.com/baizhuang/p/11405961.html