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

简单来说就是将两个单个数字的单链表相加,但是这个有一个问题,如果两个数字相加大于等于10,则将和的个位保留到此节点,下个节点多加1;

链表长度不限,且可以为空(一个链表或者两个同时为空)。

由于我是小白,做这道题时链表基本不懂,所以就直接参考了优秀解法。。。

public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
} public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
{
ListNode c1 = l1;
ListNode c2 = l2;
ListNode head = new ListNode();
ListNode p = head;
int sum = ;
while (c1!=null ||c2!=null )
{
sum /= ;
if (c1!=null)
{
sum += c1.val;
c1 = c1.next;
}
if (c2!=null)
{
sum += c2.val;
c2 = c2.next;
}
p.next = new ListNode(sum % );
p = p.next; } if (sum/==)
{
p.next = new ListNode();
}
return head.next;
}

我的理解:先声明一个空的头节点head,循环将2个链表的头节点加给值sum,并将sum除以10的余数赋值给之前申明的空节点p,加完之后再将链表的下个节点加给sum除以10的值,直到链表没有下一个节点位置,同时p指向下一节点。

当sum为10时,p的下一个节点值为1。

Add Two Numbers (c#)的更多相关文章

  1. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

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

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  4. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  5. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  6. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  9. Two Sum & Add Two Numbers

    Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...

  10. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

随机推荐

  1. JS获取Url中传入的参数

    一:后台获取,前台调用 后台: object value= Request.QueryString[key]; 前台js: $(function(){ var value="<%=va ...

  2. Jenkins+ANT+SVN快速搭建持续集成环境

    伟大的语言都从helloworld开始,这里我们用Jenkins+ANT+SVN来搭建这一套持续集成环境. 环境准备: 一台已经配置好Jenkins和Ant的机器. 项目准备: 用Ecplise建立一 ...

  3. JQuery_DOM 节点操作之创建节点、插入节点

    一.创建节点 为了使页面更加智能化,有时我们想动态的在html 结构页面添加一个元素标签,那么在插入之前首先要做的动作就是:创建节点 <script type="text/javasc ...

  4. ctags使用细节

    在src code目录中运行下面的命令(我自己使用的命令):    $ctags --langmap=c++:.h --languages=c++,c,perl,verilog -R 其中,指定cta ...

  5. 初识SSH框架

    想了解SSH的工作原理吗?还不快快来看我的新文章----ssh框架的工作原理 也许你还不知道SSH存在的意义,但只是因为在没点开这篇文章之前的事了. SSH由Strut2,Spring,Hiberna ...

  6. 分页组件 - layui.laypage

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 从PDF中提取信息----PDFMiner

    今天由于某种原因需要将pdf中的文本提取出来,就去搜了下资料,发现PDFMiner是针对 内容提取的,虽然最后发现pdf里面的文本全都是图片,就没整成功,不过试了个文本可复制的 那种pdf文件,发现还 ...

  8. .net预览功能

    1.把需要预览的文件格式转换为pdf 2.下载pdttoswf软件 3.实现预览swf功能 ok excel转pdf时会出现乱掉的问题.是excel文件的问题.调整excel文件的格式即可.

  9. 关于头文件的一些常用<meta>

    一.常见的<meta>(摘自百度) 1. 设置编码信息<meta http-equiv="Content-Type" Content="text/htm ...

  10. 关于BP网络的一些总结

    背景 前段时间,用过一些模型如vgg,lexnet,用于做监督学习训练,顺带深入的学习了一下相关模型的结构&原理,对于它的反向传播算法记忆比较深刻, 就自己的理解来描述一下BP网络. 关于BP ...