2. Add Two Numbers

本文介绍了一种解决链表数值求和问题的算法,即AddTwoNumbers算法。通过遍历两个链表并逐位相加,同时处理进位,最终返回新的链表结果。该算法巧妙地利用了链表的倒序特性,使得操作更加高效。

2. Add Two Numbers

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

 

一开始提供了ListNode 这个list, 

.val转化出来的值就是个int, 比如l1.val == 243,

.next 就是看他的下一位, l1.next == null;

这道题的输入是 243和 564  虽然是倒序,但是在运算中是一个帮助,因为运算中第一个排除的值就是答案中的个位,

 

/**
 * 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) {
        
    if(l1 == null && l2 == null){
        return null;
    }
    if(l1 == null){
        return l2;
    }
    if(l2 == null){
        return l1;
    }
        
        ListNode p1 = l1;
        ListNode p2 = l2;
        
        int carry = 0;  //用来保留2数相加/10的结果
        
        ListNode head = new ListNode(0);  //用来保留2数相加%10的结果
        ListNode result = head;
        
        while(p1 != null || p2 != null || carry != 0){
            
            int v1 = 0;
            if(p1 != null){
                
                v1 = p1.val;   //第一次是243,之后都是null 因为下一步p1.next
                p1 = p1.next;
            }
            
            int v2 = 0;
            if(p2 != null){
                
                v2 = p2.val;  //第一次是564,之后都是null 因为下一步p2.next
                p2 = p2.next;
            }
            
            int temp = v1 + v2 + carry;  //temp = 243 + 564 + 0 = 807
            carry = temp/10;    //用来保存temp/10的结果   carry =80
            head.next = new ListNode(temp % 10);  //807%10  = 7
            head = head.next; //第一个排除7
        }
        
        return result.next;
    }
}

 

如果还不理解可以debug运行一下例子

public class test7 {

    public static void main(String[]args){

        ListNode l1 = new ListNode(243);
        ListNode l2 = new ListNode(564);
        ListNode listNode = addTwoSum(l1, l2);
        System.out.println(listNode);
    }

    private static ListNode addTwoSum (ListNode l1, ListNode l2) {

        if (l1 == null && l2 == null) {
            return null;
        }

        if (l1 == null) {
            return l2;
        }

        if (l2 == null) {
            return l1;
        }

        ListNode p1 = l1;
        ListNode p2 = l2;

        int carry = 0;

        ListNode head = new ListNode(0);
        ListNode result = head;

        while (carry != 0 || p1 != null || p2 != null) {

            int v1 = 0;
            if (p1 != null) {
                v1 = p1.val;
                p1 = p1.next;
            }

            int v2 = 0;
            if (p2 != null) {
                v2 = p2.val;
                p2 = p2.next;
            }

            int tmp = v1 + v2 + carry;
            carry = tmp / 10;
            head.next = new ListNode(tmp % 10);
            head = head.next;
        }

        return result.next;
    }

    static class ListNode {

        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值