两数相加 (Add Two Numbers)

 

思路:

// @Title: 两数相加 (Add Two Numbers)
// @Author: qisiii
// @Date: 2021-07-11 22:52:34
// @Runtime: 2 ms
// @Memory: 38.6 MB
// @comment: 
// @flag: 
/**
 * 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 static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode();
        ListNode p = result;
        int jinwei=0;
        while (true) {
            int first=l1!=null?l1.val:0;
            int second=l2!=null?l2.val:0;
            int sum =p.val+first + second;
            p.val = sum % 10;
            jinwei=sum / 10;
            l1 =l1!=null? l1.next:null;
            l2 =l2!=null? l2.next:null;
            if (l1 == null && l2 == null&&jinwei==0) {
                p.next = null;
                break;
            }
            p.next = new ListNode(jinwei, new ListNode());
            p = p.next;
        }
        return result;
    }
}

+++ title = “两数相加 (Add Two Numbers)” draft = false +++

思路:通过新增的链表存储值,也算暴力吧

// @Title: 两数相加 (Add Two Numbers)
// @Author: qisiii
// @Date: 2024-04-10 22:04:48
// @Runtime: 1 ms
// @Memory: 43.2 MB
// @comment: 通过新增的链表存储值,也算暴力吧
// @flag: RED
/**
 * 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) {
        ListNode result=null;
        ListNode head=null;
        int jin=0,sum=0;
        while(l1!=null||l2!=null){
            int l1val=l1!=null?l1.val:0;
            int l2val=l2!=null?l2.val:0;
            sum=(l1val+l2val+jin)%10;
            jin=(l1val+l2val+jin)/10;
            if(result==null){
                result=new ListNode(sum);
                head=result;
            }else{
                result.next=new ListNode(sum);
                result=result.next;
            }
            if(l1!=null){
                l1=l1.next;
            }
            if(l2!=null){
                l2=l2.next;
            }
        }
        if(jin>0){
            result.next=new ListNode(jin);
        }
        return head;
    }
}

+++ title = “两数相加 (Add Two Numbers)” draft = false +++

思路:

// @Title: 两数相加 (Add Two Numbers)
// @Author: qisiii
// @Date: 2024-09-04 17:38:25
// @Runtime: 1 ms
// @Memory: 43.3 MB
// @comment: 
// @flag: 
/**
 * 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) {
        ListNode head = new ListNode(-1),result=null;
        Integer jin = 0;
        while (l1 != null || l2 != null||jin!=0) {
            int l1v = l1 != null ? l1.val : 0;
            int l2v = l2 != null ? l2.val : 0;
            int sum = l1v + l2v + jin;
            jin = sum / 10;
            head.next=new ListNode(sum%10);
            head=head.next;
            if(result==null){
                result=head;
            }
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        return result;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18