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 ...
随机推荐
- Top命令 -转
Windows下的任务管理器虽然不好用(个人更喜欢Process Explorer些),但也算方便,可以方便的查看进程,CPU,内存...也可以很容易的结束进程 没有图形化界面下的Linux,也有命令 ...
- eclipse +maven+ssm搭建矿建
记录一下搭建框架的过程1.下载最新的eclipse https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R/ec ...
- windbg运行
运行起来会提示windbg is running. BUSY 这个是正常运行的状态,只有发生异常,或者被指定断点,才会中断.
- Mysql执行大文件sql语句 -- 未测试
如果.sql文件过大,mysql会直接断开连接 解决方法: 在mysql的配置文件my.cnf 中加入 一行max_allowed_packet = 100M(该大小>=mysql.sql文件大 ...
- jQuery滚动数字
<ul class="dateList"> <li class="one"> <p class="titleName&q ...
- js 同for一样效果 (延迟)每秒循环一次 追加
<script type="text/javascript"> var j = 1; var timeID = null; function ...
- (47) odoo详细操作手册
odoo 8 详细操作手册, ERP(Odoo8.0)操作手册-v1.10(陈伟明).pdf 链接: http://pan.baidu.com/s/1hsp0bVQ 密码: r9tt 花了将近9个月时 ...
- SQL server 常用语句
SQL Server中常用的SQL语句 1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...
- Android 6编译环境搭建 (Marshmallow)
1.安装 ubuntu 14.03 尽管android推荐 ubuntu 15, 安全起见,还是装LTS的14.04,步骤跳过 2. JDK: Marshmallow 需要 JDK8 ,添个源,顺手配 ...
- 通过图片对比带给你不一样的KMP算法体验
KMP 算法,俗称“看毛片”算法,是字符串匹配中的很强大的一个算法,不过,对于初学者来说,要弄懂它确实不易. 笔者认为,KMP 算法之所以难懂,很大一部分原因是很多实现的方法在一些细节的差异.体现在几 ...