Java之两个链表数字相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

解决方案:1. 进位,以及进位数字的保存
/**
 * 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) {

        ListNode header = new ListNode(0);
        ListNode p = l1, q = l2, tmp = header;
        int curry = 0;

        while(p != null || q != null){
            int x = p != null ? p.val : 0;
            int y = q != null ? q.val : 0;
            int sum = x + y + curry;
            curry = sum /10;
            tmp.next = new ListNode(sum % 10);
            tmp = tmp.next;

            if(p != null)
                p = p.next;
            if(q != null)
                q = q.next;

        }

        if(curry > 0){
            tmp.next = new ListNode(curry);
        }
        return header.next;
    }
}
 

原文地址:https://www.cnblogs.com/chaplin-code/p/10052289.html

时间: 2025-01-18 11:41:33

Java之两个链表数字相加的相关文章

算法总结之 两个链表生成相加链表

假设链表中每一个节点的值都在0~9之间,那么链表整体就可以代表一个整数 给定两个链表的头节点head1和head2,请生成代表两个整数相加值的结果链表 传统做法 先把链表生成 整数 然后相加  这样的有个溢出问题 介绍一种做法: 利用栈结构求解: 1. 将两个链表分别从左到右遍历,遍历过程中将值压栈,这样就生成了两个链表节点的逆序栈s1 和 s2. 2.将s1和s2同步弹出,这样相当于两个链表从低位到高位一次弹出,在这个过程中生成相加链表即可,同时关注下是否有进位. 当s1 和 s2都为空时,还

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文: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

Cocos2dx制作2048(3.数字相加逻辑)

这次我们来完成整个2048的数字相加逻辑其实2048玩起来简单,做起来也简单,复杂就复杂在这整个游戏的逻辑. 1.分析向左滑动 第一轮相加步骤: 1. 单1+单2    单1=2  单2赋值为0         (单1为空,可以加任何数字) 2. 单1+单3    单1=4   单3赋值为0    (单1不为空,只能加和他相同的数字) 3. 单1+单4    单1=4   单4不变              (单1不为空,只能加和他相同的数字) 第二轮相加步骤: 1.   单2+单3    单2

[LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

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

两个链表相加

1 题目 You are giventwo linked lists representing two non-negative numbers. The digits are storedin reverse order and each of their nodes contain a single digit. Add the twonumbers and return it as a linked list. Input: (2 -> 4 -> 3)+ (5 -> 6 ->

算法总结之 两个单链表生成相加的链表

对于这个问题还有一个很好的方法: 1.将两个链表逆序,这样就可以依次得到从低到高位的数字 2.同步遍历两个逆序后链表,相加生成新链表,同时关注进位 3.当两个链表都遍历完成后,关注进位. 4. 将两个逆序的链表再逆序一遍,调整回去 返回结果链表 package TT; public class Test98 { public class Node{ public int value; public Node next; public Node(int data){ this.value=data

[LeetCode] Add Two Numbers 两个数字相加

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

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

[129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represent

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】

[129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents