<LeetCode(Java版)>Add Two Numbers

本文探讨了如何使用链表来表示两个非负整数并进行相加操作,通过两种不同的Java实现方案对比,展示了简洁高效代码的重要性。

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

题目:

  You are given two linked lists representing two non-negative numbers. 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.

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

  其实拿到这题的时候,思考的时间还是挺久的,因为要考虑的情况挺多的,
  1>首先处理错误输入
  2>然后就是当l1与l2的下面next节点不为空时怎么处理
  3>那么当l1,l2有一个next节点为空时,又怎么处理,
  4>最后进位的维持与最终是否要生成新的节点,
  这些都需要考虑进去,第一版代码相对来说挺长,但很好理解。

/**
 * 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) {
        if (l1 == null)
            return l2;
        if (l2 == null)
            return l1;
        int jinwei = 0;
        //记录需要返回的节点
        ListNode result = null;
        //记录经运算形成的节点
        ListNode node = null;
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        while (temp1 != null && temp2 != null) {
            ListNode listNode = null;
            int sum = temp1.val + temp2.val + jinwei;
            listNode = new ListNode(sum%10);
            jinwei = sum/10;
            if (node != null) {
                node.next = listNode;
            } else {
                result = listNode;
            }
            node = listNode;
            temp1 = temp1.next;
            temp2 = temp2.next;
        }
        while(temp2 != null ){
            int sum = temp2.val + jinwei;
            ListNode listNode = new ListNode(sum%10);
            jinwei = sum/10;
            node.next = listNode;
            node = listNode;
            temp2 = temp2.next;
        }
        while(temp1 != null ){
            int sum = temp1.val + jinwei;
            ListNode listNode = new ListNode(sum%10);
            jinwei = sum/10;
            node.next = listNode;
            node = listNode;
            temp1 = temp1.next;
        }
        if(jinwei == 1){
            ListNode listNode = new ListNode(1);
            node.next = listNode;
        }
        return result;
    }

  相应的Leetcode运行显示为:Runtime: 500 ms,Your runtime beats 13.03% of java coders。那么有没有有更为简洁高效的 代码呢?(下面这段代码不是本人的,我看讨论区里较为简洁且执行时间较为快捷的)

/**
 * 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) {
       int carry =0;

        ListNode newHead = new ListNode(0);
        ListNode p1 = l1, p2 = l2, p3=newHead;

        while(p1 != null || p2 != null){
            if(p1 != null){
                carry += p1.val;
                p1 = p1.next;
            }

            if(p2 != null){
                carry += p2.val;
                p2 = p2.next;
            }

            p3.next = new ListNode(carry%10);
            p3 = p3.next;
            carry /= 10;
        }

        if(carry==1) 
            p3.next=new ListNode(1);

        return newHead.next;
    }
}
相应的执行时间为Runtime: 428 ms,Your runtime beats 89.83% of java coders.

  其实个人分析而言,感觉第二种解题的方式需要学习,写出来的代码简洁且性能较优,但第一种解法与第二种解法,本人的看法是两者的执行时间应该基本相同,更确切的说,第一种解法的速度应该较第二种解法更快速,因为第二种解法的while循环中判断次数比第一种解法多,在这里会浪费一点时间,但很微小,因为这两种解法的时间复杂度都是一样的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值