题解 | #链表相加(二)#

链表相加(二)

https://www.nowcoder.com/practice/c56f6c70fb3f4849bc56e33ff2a50b6b

进步感受:

这次又是可以自己解决问题了,虽然用了stack,浪费了空间,但是,时间上是差不多的,下次可以改进。

解题思路:

最🐔仔的是,拿链表翻转,就可以实现从低开始相加了!!!!

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {

    public ListNode addInList (ListNode head1, ListNode head2) {
        if(head1 == null || head2 == null) {
            return null;
        } 

        ListNode p1 = reverseListNode(head1);
        ListNode p2 = reverseListNode(head2);
        ListNode res = new ListNode(0);
        ListNode cur = res;

        int carry = 0;
        while(p1!=null || p2!= null || carry>0) {
            // 获取当前节点相加的值
            int value1 = p1!=null?p1.val:0;
            int value2 = p2!=null?p2.val:0;
            int temp = value1 + value2 + carry;
            carry = temp / 10;

            // 生成节点添加到链表
            cur.next = new ListNode(temp%10);
            cur = cur.next;
            
            // 遍历下个节点
            p1 = p1==null? null: p1.next;
            p2 = p2==null? null: p2.next;
        }

        // 这里要小心因为我们是从小位开始添加的节点
        // 所以是000001这样的,要翻转
        return reverseListNode(res.next);
    }

    public ListNode reverseListNode(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while(cur!=null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    /**
     * 我的实现方法,空间复杂度太高
     * 遍历次数太多了。
     * @param head1 ListNode类
     * @param head2 ListNode类
     * @return ListNode类
     */
    public ListNode addInList2 (ListNode head1, ListNode head2) {
        // write code here
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        Stack<Integer> s3 = new Stack<Integer>();

        while (head1 != null) {
            s1.push(head1.val);
            head1 = head1.next;
        }

        while (head2 != null) {
            s2.push(head2.val);
            head2 = head2.next;
        }

        ListNode res = new ListNode(0);
        ListNode cur = res;
        int result = 0;
        int enter = 0;
        while(!s1.isEmpty() || !s2.isEmpty()) {
            int value1 = s1.isEmpty()? 0: s1.pop();
            int value2 = s2.isEmpty()? 0: s2.pop();
            int value = value1 + value2 + enter;
            enter = value >= 10 ? 1 : 0;
            int point = value % 10;
            s3.push(point);
        }

        if(enter == 1) {
            s3.push(enter);
        }

        while(!s3.isEmpty()) {
            cur.next = new ListNode(s3.pop());
            cur = cur.next;
        }

        return res.next;
    }


}
全部评论

相关推荐

行云流水1971:你的简历已经有不错的内容基础,但在岗位匹配度、成果量化、逻辑分层上还有优化空间,我结合产品 / 金融科技类岗位偏好帮你调整: 一、现有问题 & 优化方向 信息冗余:课程 / 学生工作与目标岗位关联弱,可精简; 成果颗粒度不足:部分数据缺少 “对比基准”(比如 “效率提升” 没说之前的情况); 岗位标签弱:产品岗核心能力(如需求闭环、PRD 撰写)体现不够突出。 二、优化后简历(以 “金融科技产品岗” 为例) 教育经历 2023.09-2027.06 郑州轻工业大学(公办一本) | 软件工程 | 本科 核心课程:Java 程序设计、数据库原理、Python(匹配产品岗 “技术理解” 需求) 学习成果:专业核心课 90+,获校级一等奖学金; 学生工作:院学生会主席,统筹 6 场校级活动(覆盖 2000 + 人次),锻炼跨部门协作与项目统筹能力。 实习经历
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务