当前位置 : 主页 > 编程语言 > c++ >

【Leetcode】Add Two Numbers

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode result = new ListNode(0); int flow = 0; List res = new ArrayList (); while(l1!=null || l2!=null){ int l1Val = 0; int l2Val = 0; if(l1!=null){ l1Val = l1
gistfile1.txt
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {		 
		 ListNode result = new ListNode(0);		
		 int flow = 0;
		 List
 
   res = new ArrayList
  
   (); while(l1!=null || l2!=null){ int l1Val = 0; int l2Val = 0; if(l1!=null){ l1Val = l1.val; } if(l2!=null){ l2Val = l2.val; } int curVal = l1Val + l2Val + flow > 10 ? (l1Val + l2Val + flow)/10: (l1Val + l2Val + flow)%10; int curFlow = l1Val + l2Val + flow >= 10?(l1Val + l2Val + flow)/10:0; flow = curFlow; l1 = l1.next; l2 = l2.next; res.add(curVal); } ListNode node = result; for(int i=0;i<=res.size()-2;i++){ node.val = res.get(i); node.next = new ListNode(res.get(i+1)); node = node.next; } return result; }
  
 
网友评论