NC317 链表相加(一)(二)

描述

牛客给定两个非空链表逆序存储的的非负整数,每个节点只存储一位数组。
请你把两个链表相加以下相同方法返回链表,保证两个数都不会以 0 开头。
数据范围:1≤n≤10 ^5
,每个节点的值都满足0≤val≤9

示例1
输入:
{2,5,6},{5,6,1}
返回值:
{7,1,8}

示例2
输入:
{0},{1,2,3,4,5,6}
返回值:
{1,2,3,4,5,6}

示例3
输入:
{9,9,9},{9,9,0}
返回值:
{8,9,0,1}

首先存入所有值到队列中去(加的数据为先进先出类型),如果加的值大于9,则标签一下,下一轮进1
所有存入的值需要取余10
最后如果所有队列为空,但是标签为true,则说明最后一个值需要进1

import java.util.*;

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

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param l1 ListNode类
     * @param l2 ListNode类
     * @return ListNode类
     */
    public ListNode ListAdd (ListNode l1, ListNode l2) {
        // write code here
        Queue<Integer> q1 = new ArrayDeque<>();
        Queue<Integer> q2 = new ArrayDeque<>();
        while (l1 != null || l2 != null) {
            if (l2 == null) {
                q1.add(l1.val);
                l1 = l1.next;
            } else if (l1 == null) {
                q2.add(l2.val);
                l2 = l2.next;
            } else {
                q1.add(l1.val);
                q2.add(l2.val);
                l1 = l1.next;
                l2 = l2.next;
            }
        }
        // 如果需要进位,则flag=1
        Boolean flag = false;
        ListNode result = new ListNode(-1);
        ListNode move = result;
        while (!q1.isEmpty() || !q2.isEmpty()) {
            Integer poll1;
            Integer poll2;
            if (q1.isEmpty() && !q2.isEmpty()) {
                poll1 = 0;
                poll2 = q2.poll();
            } else if (!q1.isEmpty() && q2.isEmpty()) {

                poll1 = q1.poll();
                poll2 = 0;
            } else {
                poll1 = q1.poll();
                poll2 = q2.poll();
            }
            int res = poll1 + poll2;
            if (flag) {
                // 为了得到加了以后的是否要进位
                res = res + 1;
                move.next = new ListNode(res%10);
                flag = false;
            } else {
                move.next = new ListNode(res%10);
            }
            if (res > 9) {
                // 下一轮要进位,所以写在后面
                flag = true;
            }
            move = move.next;
        }
        if(flag){
            move.next = new ListNode(1);
            move = move.next;
        }
        return result.next;
    }
}

普通方法:判空并计算,保留一个进值位记录

import java.util.*;
public class Solution {
    public ListNode ListAdd (ListNode l1, ListNode l2) {
        // write code here
        ListNode result = new ListNode(-1);
        ListNode head = result;
        int bit = 0;
        while(l1!=null&& l2!=null){
            int v1 = l1.val;
            int v2 = l2.val;
            int res = (v1+v2+bit)%10;
            bit = (v1+v2+bit)/10;
            ListNode curr = new ListNode(res);
            head.next = curr;
            head = head.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        if(l1!=null){
            head.next = getOtherValue(l1,bit,head);
            head = head.next;
        }
        if(l2!=null){
            head.next = getOtherValue(l2,bit,head);
            head = head.next;
        }
        
        return result.next;
    }
    ListNode getOtherValue(ListNode head,int bit,ListNode pre){
        ListNode result = pre;
        while(head!=null){
            int res = (head.val+bit)%10;
            bit = (head.val+bit)/10;
            ListNode curr = new ListNode(res);
            pre.next = curr;
            pre = pre.next;
            head = head.next;
        }
        if(bit>0){
            ListNode curr = new ListNode(bit);
            pre.next = curr;
        }
        return result.next;
    }
}

简单方法:
定义一个值,如果需要进值,则该值留1即可

 public ListNode ListAdd (ListNode l1, ListNode l2) {
        int carray = 0;
        ListNode head = new ListNode(0);
        ListNode pre = head;
        while(l1 != null || l2 != null || carray != 0) {
            int sum = carray;
            if(l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if(l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            pre.next = new ListNode(sum%10);
            pre = pre.next;
            carray = sum/10;    
        }
        return head.next;
    }

(二)
描述
牛客
假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。
给定两个这种链表,请生成代表两个整数相加值的结果链表。
数据范围:0≤n,m≤1000000,链表任意值 0≤val≤9
要求:空间复杂度 O(n),时间复杂度 O(n)

例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。
在这里插入图片描述

示例1
输入:
[9,3,7],[6,3]
返回值:
{1,0,0,0}
说明:
如题面解释    

示例2
输入:
[0],[6,3]
返回值:
{6,3}

备注:
在这里插入图片描述
思路:继上一题简单思路。首先用栈保存值,由于加的值从最后一位开始。
最后需要反转链表

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     *
     * @param head1 ListNode类
     * @param head2 ListNode类
     * @return ListNode类
     */
    public ListNode addInList (ListNode listnode1, ListNode listnode2) {
        // write code here
        int flag = 0;
        // 用栈保存
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while (listnode1 != null || listnode2 != null) {
            if (listnode1 != null) {
                stack1.push(listnode1.val);
                listnode1 = listnode1.next;
            }
            if (listnode2 != null) {
                stack2.push(listnode2.val);
                listnode2 = listnode2.next;
            }
        }
        Stack<Integer> res = new Stack<>();
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            if (!stack1.isEmpty()) {
                Integer pop = stack1.pop();
                flag += pop;
            }
            if (!stack2.isEmpty()) {
                Integer pop = stack2.pop();
                flag += pop;
            }
            res.add(flag % 10);
            flag = flag / 10;
        }
        if (flag > 0) {
            res.add(1);
        }
        // 反转链表
        ListNode result = new ListNode(-1);
        ListNode pre = result;
        while (!res.isEmpty()) {
            pre.next = new ListNode(res.pop());
            pre = pre.next;
        }
        return result.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值