Leetcode-445. Add Two Numbers II

本文介绍了一种解决两个非空链表表示的非负整数相加的问题,并给出了具体的实现方法。通过将链表转换为字符串进行加法运算,避免了整数溢出的问题,最终返回相加后的链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发优快云,mcf171专栏。

博客链接:mcf171的博客

——————————————————————————————

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
这个题目一开始想的挺简单的,考虑的是非负整数,所以先转换为整数然后相加,然后再转换成linkedList,但是没想到会溢出,所以干脆转换为字符串,然后做字符串加法,时间复杂度O(m+n)。 Your runtime beats 74.72% of java submissions.

/**
 * 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) {
        String number1 = getNumber(l1);
        String number2 = getNumber(l2);
        int i = number1.length() - 1;
        int j = number2.length() - 1;
        boolean support = false;
        ListNode node = null;
        while(i >= 0 && j >= 0){
            int result = (number1.charAt(i) - '0') + (number2.charAt(j) - '0');
            if(support) {result += 1; support = false;}
            if(result >= 10) {support = true;result %= 10;}
            ListNode temp = node;
            node = new ListNode(result);
            node.next = temp;
            i--;j--;
        }
        while(i >= 0){
            ListNode temp = node;
            int result = (number1.charAt(i) - '0');
            if(support) {result += 1; support = false;}
            if(result >= 10) {support = true;result %= 10;}
            node = new ListNode(result);
            node.next = temp;
            i--;
        }
        while(j >= 0){
            ListNode temp = node;
            int result = (number2.charAt(j) - '0');
            if(support) {result += 1; support = false;}
            if(result >= 10) {support = true;result %= 10;}
            node = new ListNode(result);
            node.next = temp;
            j--;
        }
        
        if(support) {
            ListNode temp = node;
            node = new ListNode(1);
            node.next = temp;
        }
        
        return node;
    }
    
    private String getNumber(ListNode node){
        StringBuffer sb = new StringBuffer("");
        while(node != null){
            sb.append(node.val);
            node = node.next;
        }
        return sb.toString();
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值