题目:
第一次提交:失败
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 }
问题:代码重复性太高,变量多而复杂
第三次提交: 失败超时
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 static ListNode calculate(ListNode l3,int upper){ 11 ListNode tempNode = l3; 12 int dt=0; 13 while(l3!=null){ 14 dt = l3.val + upper; 15 upper =0; 16 upper = dt/10; 17 dt = dt%10; 18 l3.val = dt; 19 if(upper!=0&&l3.next==null){ 20 l3.next = new ListNode(upper); 21 l3.next.next=null; 22 break; 23 } 24 l3 = l3.next; 25 } 26 return tempNode; 27 } 28 29 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 30 31 ListNode result = null,p = null,q=null,tempNode=null; 32 int data=0,upper=0; 33 boolean isHeadNode =true; 34 while(l1!=null||l2!=null){ 35 if(l1==null){ 36 if(upper!=0) 37 l2 = calculate(l2,upper); 38 q.next = l2; 39 }else if(l2==null){ 40 if(upper!=0) 41 l1 = calculate(l1,upper); 42 q.next = l1; 43 }else{ 44 //合并处理 45 data = l1.val + l2.val + upper; 46 upper = 0; 47 upper = data/10; 48 data = data%10; 49 p = new ListNode(data); 50 p.next = null; 51 l1 = l1.next; 52 l2 = l2.next; 53 if(isHeadNode){ 54 isHeadNode = false; 55 result = p; 56 }else{ 57 q.next = p; 58 } 59 q = p; 60 } 61 } 62 if(upper!=0){ 63 tempNode = new ListNode(upper); 64 tempNode.next = null; 65 for(;result.next!=null;result=result.next); 66 result.next = tempNode; 67 68 } 69 return result; 70 } 71 }
第四次提交 : 优化通过
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 11 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 12 ListNode root =null, preNode =null, endNode =null; 13 boolean isFirstNode = true; 14 int upper = 0; 15 int data = 0; 16 17 while(l1!=null||l2!=null){ 18 //do l1 l2 19 if(l1==null){ 20 //do l2 21 for(;l2!=null;l2=l2.next){ 22 data = l2.val + upper; 23 upper = data/10; 24 data = data%10; 25 preNode = new ListNode(data); 26 preNode.next = null; 27 endNode.next = preNode; 28 endNode = preNode; 29 } 30 }else if(l2==null){ 31 //do l1 32 for(;l1!=null;l1=l1.next){ 33 data = l1.val + upper; 34 upper = data/10; 35 data = data%10; 36 preNode =new ListNode(data); 37 preNode.next = null; 38 endNode.next = preNode; 39 endNode = preNode; 40 } 41 }else{ 42 //do l1 l2 43 data = l1.val + l2.val + upper; 44 upper = data/10; 45 data = data%10; 46 preNode = new ListNode(data); 47 preNode.next = null; 48 if(isFirstNode){ 49 root = preNode; 50 endNode = preNode; 51 isFirstNode = false; 52 }else{ 53 endNode.next = preNode; 54 endNode = preNode; 55 } 56 l1 = l1.next; 57 l2 = l2.next; 58 } 59 } 60 //处理进位upper 61 if(upper!=0){ 62 endNode.next = new ListNode(upper); 63 endNode.next.next = null; 64 } 65 return root; 66 } 67 }