用链表表示的两个数相加

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

Output: 7 -> 0 -> 8

2 分析

该题目属于链表的相加,需要注意的有:

(1) 考虑两个链表的长度,尤其是链表为空时也能处理。

(2) 每个结点只表示一位数字。

(3) 当链表末尾结点相加后若有进位,则需要申请新的结点存储信息。

3 实现

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *    int val;
 *    ListNode *next;
 *    ListNode(int x) : val(x), next(NULL) {}
 * };
 */

intlistLength(ListNode* head)
{
      return head ? 1 + listLength(head ->next) : 0;
}

ListNode*Solution::addTwoNumbers(ListNode *l1, ListNode *l2)
{
      if (listLength(l1) < listLength(l2))
      {
             return addTwoNumbers(l2, l1);
      }
      ListNode *head1 = l1, *head2 = l2;
      int inc = 0;
      bool isEnd = false;
      while (head2)
      {
             int val = head1 -> val + head2-> val + inc;
             head1 -> val = val % 10;
             inc = val / 10;
             if (head1 -> next)
             {
                    head1 = head1 -> next;
             }
             else
             {
                    isEnd = true;
             }
             head2 = head2 -> next;
      }
      while (inc)
      {
             int val = isEnd ? inc : head1 ->val + inc;
             if (isEnd)
             {
                    head1 -> next = newListNode(val % 10);
             }
             else
             {
                    head1 -> val = val % 10;
             }
             inc = val / 10;
             if (head1 -> next)
             {
                    head1 = head1 -> next;
             }
             else
             {
                    isEnd = true;
             }
      }
      return l1;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 22:13:24

用链表表示的两个数相加的相关文章

Leetcode算法系列(链表)之两数相加

Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和.您可以假设除了数字 0 之外,这两个数都不会以 0 开头.示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807 链接:https://le

两个数相加

给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 package com.leetcode.part1; /** * @aut

实现两个数相加不用四则运算

分析:实现两个是相加不用四则运算,根据计算机中的运算不用四则运算那么肯定是位运算了. (以下分析来自剑指offer)比如我们计算5+17=22这个结果,世界上,我们可以分为3个步骤计算,第一步各位数相加不进位,此时的结果是12(个位相加不进位是2,十位相加是1),所以结果是12: 第二步做进位,5+7有进位,进位值是10:第三步是把前两步计算结果加起来.12 + 10 = 22. 那么运用位运算二进制的数字也可以这么考虑,5是二进制的101,17是二进制的10001.试着把计算分为3步走,第一步

2-36进制的 两个数相加

最近在很多场合都看见设计模式的影子,一直以来,都投入主要时间在搞算法与数据结构,很来发现设计模式真的很重要.有的时候代码的可维护.可重用.可扩展确实胜过单纯的算法效率高.所以拾起大牛书籍<大话设计模式>同时参考网上诸大牛的博客,开始我的设计模式之旅.由于平时编程时用C/C++,现在是Java,也练练Java语法. 今天先介绍一下命令模式. 概念: 命令模式(Command):将一个请求封装成一个对象,从而使你可用不同的请求对象对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作.

CUDA学习(三)之使用GPU进行两个数相加

在CPU上定义两个数并赋值,然后使用GPU核函数将两个数相加并返回到CPU,在CPU上显示 #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <iomanip> #include <iostream> #include <stdio.h> using namespace std; //检测GPU bool CheckCUDA(void)

用链表实现两个数相加

说明:使用链表实现两个数的和,数的高位存储在链表的头部,最后输出结果.注:使用了翻转链表的功能. #include<stdio.h> #include<stdlib.h> struct Node { int value; Node *next; }; Node *reverseList(Node *head) { Node *pCur=head; Node *pPre=NULL; Node *rHead=NULL; while(pCur!=NULL) { Node *pNext=p

[LeetCode]2. 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 1: 找出两个数相加等于给定数 two sum

问题描述 对于一个给定的数组,找出2个数,它们满足2个数的和等于一个特定的数,返回这两个数的索引.(从1开始) Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target,

链表表示的两数相加

给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 代码一: 1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 2 ListNode res