Add Two Numbers (c#)
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#)的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [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 ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- Two Sum & Add Two Numbers
Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
随机推荐
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass() word对象 2. Microsoft.Offic ...
- hdu4087ALetter to Programmers(三维旋转矩阵)
参考 三维旋转矩阵 + 矩阵加速 这个还要用到仿射变换. 平移 translate tx ty tz 1 0 0 tx 0 1 0 ty 0 0 1 tz 0 0 0 1 缩放 scale kx ky ...
- Sublime text3 常用插件 安装
1 安装插件前的准备工作 首先确保你的Sublime Text3编辑器为官方版(非破解版),建议下载官网的便携版本(好处多多). 然后安装插件管理工具(Package Control) 1.1 打开S ...
- javascript事件之:谈谈自定义事件(转)
http://www.cnblogs.com/pfzeng/p/4162951.html 对于JavaScript自定义事件,印象最深刻的是用jQuery在做图片懒加载的时候.给需要懒加载的图片定义一 ...
- JQ_浏览器窗口改变触发
$(window).resize(function () { //当浏览器大小变化时 alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(docume ...
- 基于Eclipse+Cordova的Android Hybrid应用开发环境搭建
环境说明 操作系统:Windows 7 64位 Eclipse版本:4.5.2 Release(eclipse-jee-mars-2) JDK版本:1.8 搭建步骤 1.从http://www.ecl ...
- VB用windows API激活子窗体
http://files.cnblogs.com/files/liuzhaoyzz/%E6%BF%80%E6%B4%BB%E5%AD%90%E7%AA%97%E4%BD%93.rar setforeg ...
- 转:Linux内部的时钟处理机制全面剖析
Linux内部的时钟处理机制全面剖析 在 Linux 操作系统中,很多活动都和时间有关,例如:进程调度和网络处理等等.所以说,了解 Linux 操作系统中的时钟处理机制有助于更好地了解 Linux 操 ...
- css学习笔记 9
两列定宽和两列宽度自适应结构: 在ie7及以下,container的宽度和两列子元素的宽度设置为具体值或百分比的任意组合时,两列子元素即使浮动,container的高度也能自适应:其他浏览器需要为co ...
- 补PSP进度(10.28-11.3)
本周PSP进度 10月31号 内容 开始时间 结束时间 打断时间 净时间 看蛋白质相互作用论文 8:40 10:35 约12m 103m 分析约跑功能 13:20 13:55 0 35m 练习VSL2 ...