Add Two Numbers

本文介绍了一种解决链表加法问题的方法,通过将链表转换为整数进行加法运算,再将结果转换回链表形式。文章详细阐述了解决方案的步骤,包括如何处理进位、不同长度链表的加法等细节。

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

先说下自己的感想:

    这道题耗费4个多小时的时间,思路大致行得通。一经上手,发现遗忘的东西很多,加上以前一直用的是Eclipse,对于好多知识点都记得不太清楚,甚至忘记!!!

----------------------------------------------------------------------------------------------------------------------------------

原题:

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 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.

Example

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

翻译:

给定两个非空链表,表示两个非负整数。这些数字以相反的顺序存储,每个节点都包含一个数字。添加两个数字并将其作为一个链表返回。

你可以假设这两个数字不包含任何前导零,除了数字0本身。

/**
 * 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) {
    }
}

----------------------------------------------------------------------------------------------------------------------------------

大致思路:

    1、先将l1与l2中的数字分别一一取出,这里存放入ArrayList(list1 与 list2)中;

    2、判断 list1 与 list2 的长度是否相等:

        相等:挨个加起来将值存入 list3 中

        不相等:将长度短的挨个加起来将值存入 list3 ,并将长的那个list多余的放入进去即可

    3、判断list3中的每个值是否大于10:

        大于:将其修改为其值 - 10,

            若有下一位,将下一位的值加 1 ,

            若没有下一位,list3 中追加 1

        不大于:跳过

    4、将 list3 中的值倒叙存入ListNode l3 中

        过程如图所示(例如 7 0 8):

            

    答案(通过了LeetCode的全部test):

/**
 * 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) {
        
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        int num1 = 0;
        int num2 = 0;
        
        while(l1 != null){
            list1.add(l1.val);
            l1 = l1.next;
            num1++;
        }
        
        while(l2 != null){
            list2.add(l2.val);
            l2 = l2.next;
            num2++;
        }
        
        List<Integer> list3 = new ArrayList<>();
        if(num1 == num2){
            for(int i = 0; i < num1 ;i++){
                list3.add( list1.get(i) + list2.get(i) );
            }
        }else if(num1 > num2) {
            for(int i = 0; i < num2 ;i++){
                list3.add( list1.get(i) + list2.get(i) );
            }
            for(int i = num2 ; i < num1 ;i++){
                list3.add( list1.get(i));
            }
        }else{
            for(int i = 0; i < num1 ;i++){
                list3.add( list1.get(i) + list2.get(i) );
            }
            for(int i = num1 ; i < num2 ;i++){
                list3.add( list2.get(i));
            }
        }
        
        for(int i = 0 ; i < list3.size() ; i++){
            int temp = list3.get(i);
            if(temp >= 10 && i != list3.size() - 1){
                list3.set(i+1,list3.get(i+1)+1);
                list3.set(i,temp - 10);
            }if(temp >= 10 && i == list3.size() - 1){
                list3.add(1);
                list3.set(i,temp - 10);
            }
        }
        
        ListNode l3 = new ListNode(list3.get(list3.size()-1));
        for(int i = list3.size() - 2 ; i >= 0 ; i--){
            ListNode l4 = new ListNode(list3.get(i));
            l4.next = l3;
            l3 = l4;
        }
        
        return l3;
        
    }
}

结果:

Submission Detail

1562 / 1562 test cases passed.
Status: 
Accepted
Runtime: 68 ms
Submitted: 0 minutes ago


标准答案:这里就不放了,直接给个链接吧:https://leetcode.com/articles/add-two-numbers/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值