Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.02 KB

杨新峰-20180713.md

File metadata and controls

56 lines (42 loc) · 1.02 KB

【leetcode 算法题】

Add Two Numbers

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode ret = null;
        ListNode curr = null;
        int i = 0;
        while(l1!=null || l2!=null || i>0){
            l1 = l1==null?new ListNode(0):l1;
            l2 = l2==null?new ListNode(0):l2;

            int sum = l1.val+l2.val+i;
            ListNode temp = new ListNode(sum%10);
            i=sum/10;

            if(ret==null) {
                ret = temp;
                curr = ret;
            }else {
                curr.next = temp;
                curr = curr.next;
            }

            l1 = l1.next;
            l2 = l2.next;
        }

        return ret;
    }

}

【英文文章】

暂无

【技术技巧】

暂无

【技术分享】

暂无