2. Add Two Numbers

161 阅读2分钟

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode p=new ListNode();
        ListNode head=p;
        int carry=0;
        while(l1!=null || l2!=null){
            int v1=0,v2=0;
            if(l1!=null){
                v1=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                v2=l2.val;
                l2=l2.next;
            }
            int sum=carry+v1+v2;
            int val=sum%10;
            carry=sum/10;
            ListNode tmp=new ListNode(val);
            p.next=tmp;
            p=p.next;

        }
        if(carry>0){
            ListNode tmp=new ListNode(carry);
            p.next=tmp;
            p=p.next;
        }
        p.next=null;
        return head.next;

    }
}

67. Add Binary

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

Constraints:

  • Each string consists only of '0' or '1' characters.

  • 1 <= a.length, b.length <= 10^4

  • Each string is either "0" or doesn't contain any leading zero.

    class Solution { public String addBinary(String a, String b) { int l1=a.length()-1,l2=b.length()-1; StringBuilder sb=new StringBuilder(); int carry=0; while(l1>=0 || l2>=0){ int v1=0,v2=0; if(l1>=0){ v1=a.charAt(l1)-'0'; l1--; }

            if(l2>=0){
               v2=b.charAt(l2)-'0';
                l2--;
            }
            int val=(carry+v1+v2)%2;
            carry=(carry+v1+v2)/2;
            sb.insert(0,val);
    
        }
        if(carry>0){
            sb.insert(0,carry);
        }
        return sb.toString();
    
    }
    

    }

415. Add Strings

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.

  2. Both num1 and num2 contains only digits 0-9.

  3. Both num1 and num2 does not contain any leading zero.

  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

    class Solution { public String addStrings(String num1, String num2) { if(num1==null ||num1.length()==0) return num2; if(num2==null ||num2.length()==0) return num1; int l1=num1.length()-1;int l2=num2.length()-1; int carry=0; StringBuilder sb=new StringBuilder(); while(l1>=0 || l2>=0){ int v1=0, v2=0; if(l1>=0) { v1=num1.charAt(l1)-'0'; l1--; } if(l2>=0){ v2=num2.charAt(l2)-'0'; l2--; } int val=(v1+v2+carry)%10; carry=(v1+v2+carry)/10; sb.insert(0,val);

        }
        if(carry>0){
            sb.insert(0,carry);
        }
       return sb.toString();
    
    }
    

    }

445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
         //reverse
        l1=reverseNode(l1);
        l2=reverseNode(l2);
        ListNode head=new ListNode();
        int carry=0;
        while(l1!=null || l2!=null){
            int v1=0,v2=0;
            if(l1!=null){
                v1=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                v2=l2.val;
                l2=l2.next;
            }
            int sum=(carry+v1+v2)%10;
            carry=(carry+v1+v2)/10;
            ListNode tmp=new ListNode(sum);
            tmp.next=head.next;
            head.next=tmp;

        }
        if(carry>0){
            ListNode tmp=new ListNode(carry);
            tmp.next=head.next;
            head.next=tmp;

        }

        return head.next;

    }

    ListNode reverseNode(ListNode head){
        if(head==null || head.next==null){
            return head;
        }
        ListNode p1=head;
        ListNode p2=head.next;
        p1.next=null;
        while(p2!=null){
            ListNode tmp=p2.next;
            p2.next=p1;
            p1=p2;
            p2=tmp;
        }
        return p1;

    }
}