讨论/《中级算法》 - 两数相加/
《中级算法》 - 两数相加
共 4 个回复

执行用时:
2 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.7 MB, 在所有 Java 提交中击败了73.88%的用户

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode nodes=new ListNode();
        ListNode head=new ListNode();
        head.next=nodes;
        boolean jw=false;
        while(l1!=null||l2!=null){
            int x=(l1==null?0:l1.val)+(l2==null?0:l2.val)+(jw?1:0);
            if(x>=10){
                jw=true;
                x=x%10;
            }else{
                jw=false;
            }
            ListNode node=new ListNode(x);
            nodes.next=node;
            nodes=nodes.next;
            if(l1!=null){
                l1=l1.next;
            }
             if(l2!=null){
                l2=l2.next;
            }
        }
        if(jw){
      
            nodes.next=new ListNode(1);
            nodes=nodes.next;
        }
        return head.next.next;
    }
}
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {;

        ListNode sumList = new ListNode(0), res = sumList;
        int addOne = 0;
        while(l1 != null || l2 != null) {
            int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + addOne;
            addOne = sum > 9 ? 1 : 0;
            ListNode newNode = new ListNode(sum % 10);
            sumList.next = newNode;
            sumList = newNode;

            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }

        if (addOne > 0) {
            ListNode newNode = new ListNode(1);
            sumList.next = newNode;          
        }

        return res.next;

    }
}

执行用时:2 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38.7 MB, 在所有 Java 提交中击败了73.38%的用户

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode pre = dummy;
        int carry = 0;
        while (carry >0 || l1 != null || l2 != null){
            int v1 = 0;
            int v2 = 0;
            if(l1 != null){
                v1 = l1.val;
                l1 = l1.next;
            }
            if(l2 != null){
                v2 = l2.val;
                l2= l2.next;
            }
            int sum = v1 + v2 + carry;
            int val = sum % 10;
            carry = sum / 10;
            ListNode node = new ListNode(val);
            pre.next = node;
            pre = pre.next;
        }
        if(l1 != null){
            pre.next = l1;
        }
        if(l2 != null){
            pre.next = l2;
        }
        return dummy.next;
    }

直接暴力加了

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        ListNode result = new ListNode(0);
        ListNode tempResult = result;
        int next = 0;
        while(temp1 != null && temp2 !=null){
            int num1 = temp1.val;
            int num2 = temp2.val;
            int sum = num1 + num2;
            int data = (sum + next) % 10 ;
            next = (sum + next) / 10;
            tempResult.next = new ListNode(data);
            tempResult = tempResult.next;
            temp1 = temp1.next;
            temp2 = temp2.next;
        }
        while(temp1 != null){
            int sum = temp1.val;
            int data = (sum + next) % 10;
            next = (sum + next) / 10;
            tempResult.next = new ListNode(data);
            tempResult = tempResult.next;
            temp1 = temp1.next;
        }
        while(temp2 != null){
            int sum = temp2.val;
            int data = (sum + next) % 10;
            next = (sum + next) / 10;
            tempResult.next = new ListNode(data);
            tempResult = tempResult.next;
            temp2 = temp2.next;
        }
        if(next != 0){
            tempResult.next = new ListNode(next);
        }
        result = result.next;
        return result;
    }
}