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 ...
随机推荐
- phpstorm取消强制换行
1 取消phpstorm右侧竖线显示 2 取消格式化代码时 自动换行
- node与socket.io搭配小例子-转载
//服务端代码 io = require('socket.io').listen(app), fs = require('fs'), cookie=require('cookie'); request ...
- CentOS6.4x64_安装Qt5
1.安装Qt5.3.2x86 由于 OS是x64,Qt是x86,∴需要在 系统中安装相关的32位的程序(比如 报错"bad elf interpreter"的时候 就需要" ...
- 一个div相对于外层的div水平和垂直居中
我自己感觉,第四种比较常用 <title>无标题文档</title><style> .parent { width:800px; ...
- 3个sprint的团队贡献分
第一次冲刺贡献分 成员名字 贡献分 101丘娟 23 108周诗琦 26 107杨晓霞 24 124陈程 27 第二次冲刺贡献分 成员名字 贡献分 101丘娟 23 108周诗琦 27 ...
- 基本类型和引用类型调用是的区别(Object.create)
var person = { name : 'jim', address:{ province:'浙', city:'A' } } var newPerson = Object.create(pers ...
- hihocoder挑战赛26
某蒟蒻成功的·写出了T1并rank16...小岛的题目真难... 传送门:http://hihocoder.com/contest/challenge26 T1 如果你想要暴力枚举的话显然是不行的 如 ...
- 初始通过 FastClick.notNeeded 方法判断是否需要做后续相关处理
其实前面几篇文章大家都遇到一些错误,很多时候呢,我并没有直接回复解决方案,不是LZ不想告诉大家,如果不想那就不写这个了,估计博客园啊CSDN啊那么多写博客的,很少有人把现用框架分享出来,既然分享就毫不 ...
- intent参数的回传
1. public class MainActivity extends Activity { private EditText editText1, editText2, editText3; pr ...
- dstoon系统中学习
$r = $db->get_one("SELECT * FROM {$DT_PRE}company WHERE username='$pay_user'");注意:usern ...