首页 > 其他 > 详细

[LeetCode]--Add Two Number

时间:2014-01-24 22:56:21      阅读:559      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/


public
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode p = head; int carry = 0; while(l1 != null || l2 != null){ int result = 0; int va = (l1 == null)? 0: l1.val; int vb = (l2 == null)? 0: l2.val; result = (va+vb+carry)%10; carry = (va+vb+carry)/10; p.next = new ListNode(result); p = p.next; l1 = (l1 == null ? null : l1.next); l2 = (l2 == null ? null : l2.next); } if(carry != 0){ p.next = new ListNode(carry); } return head.next; } }
bubuko.com,布布扣

[LeetCode]--Add Two Number

原文:http://www.cnblogs.com/RazerLu/p/3532649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!