添加两个表示为图形的数字

时间:2016-05-24 15:59:15

标签: java algorithm linked-list nodes

我正试图在leetcode上解决这个问题:https://leetcode.com/problems/add-two-numbers/ 我的总体战略是:

  1. 添加每个列表中的第一个数字
  2. 继续添加数字,直到一个节点(或两者)位于最后一个元素
  3. 继续从包含更多元素的列表中添加
  4. 返回值
  5. 我运行的一项测试是使用l1 = [1,8,6]l2 = [1,2,3]。我的回答是[0,0,0],正确答案是[2,0,0,1]

    我添加了评论以提高可读性。

    /**
    * Definition for singly-linked list.
    * public class ListNode {
    *     int val;
    *     ListNode next;
    *     ListNode(int x) { val = x; }
    * }
    */
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int carry = 0;
            int ii = l1.val + l2.val;
            ListNode nd;
            if (ii >= 10){ // adding the first digits
                carry = ii / 10;
                nd = new ListNode(ii%10);
            } else {
                nd = new ListNode(ii);
            }
            ListNode lst = nd;
            // keep adding corresponding digits until one of the lists is about to end
            while (l1.next != null && l2.next != null){ 
                l1 = l1.next;
                l2 = l2.next;
                int sum = l1.val + l2.val + carry;
                if (sum >= 10){
                    carry = sum/10;
                    nd.next = new ListNode(sum%10);
                    nd = nd.next;
                }
                else {
                    nd.next = new ListNode(sum);
                    nd = nd.next;
                    carry = 0;
                }
            }
    
            if (l1.next == null && l2.next != null){
                // adding the last digit of l1 to the corresponding l2 digit
                int sum = l1.val + l2.val + carry;
                if (sum >= 10){
                    carry = sum/10;
                    nd.next = new ListNode(sum%10);
                    nd = nd.next;
                }
                else {
                    nd.next = new ListNode(sum);
                    nd = nd.next;
                }
                l2 = l2.next;
                while (l2.next != null){
                    int sum1 = l2.val + carry;
                    if (sum1 >= 10){
                        carry = sum1/10;
                        nd.next = new ListNode(sum1%10);
                        nd = nd.next;
                    } else {
                        carry = 0;
                        nd.next = new ListNode(sum1);
                        nd = nd.next;
                    }
                    l2 = l2.next;
                }
                int neww = l2.val + carry;
                if (neww >= 10){ // adding last digit to total sum
                    carry = neww/10;
                    nd.next = new ListNode(neww%10);
                    nd = nd.next;
                } else {
                    carry = 0;
                    nd.next = new ListNode(neww);
                    nd = nd.next;
                }
            }
            else if (l2.next == null && l1.next != null) {
                int sum = l1.val + l2.val + carry;
                if (sum >= 10){
                    carry = sum/10;
                    nd.next = new ListNode(sum%10);
                    nd = nd.next;
                }
                else {
                    nd.next = new ListNode(sum);
                    nd = nd.next;
                    carry = 0;
                }
                l1 = l1.next;
                while (l1.next != null){
                    int sum2 = l1.val + carry;
                    if (sum2 >= 10){
                        carry = sum2/10;
                        nd.next = new ListNode(sum2%10);
                        nd = nd.next;
                    } else {
                        carry = 0;
                        nd.next = new ListNode(sum2);
                        nd = nd.next;
                    }
                    l1 = l1.next;
                }
                int neww = l1.val + carry;
                if (neww >= 10){
                    carry = neww/10;
                    nd.next = new ListNode(neww%10);
                    nd = nd.next;
                } else {
                    carry = 0;
                    nd.next = new ListNode(neww);
                    nd = nd.next;
                }
            }
            else { // both lists have same size
                int sum = l1.val + l2.val + carry;
                if (sum >= 10){
                    carry = sum/10;
                    nd.next = new ListNode(sum%10);
                    nd = nd.next;
                }
                else {
                    nd.next = new ListNode(sum);
                    nd = nd.next;
                    carry = 0;
                }
            }
            return lst.next;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

试试这个:

public List<Integer> solution(List<Integer> a, List<Integer> b) {
    if (a.length != b.length)
        throw new AssertionError();

    List<Integer> result = new LinkedList<>(a.length);
    for (int i = 0; i < a.length; i++) {
        result.add(a.get(i) + b.get(i));
    } 
    return result;
}