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 ...
随机推荐
- 译\Node.js应用的持续部署
Node.js应用的持续部署 翻译前 翻译自:https://blog.risingstack.com/continuous-deployment-of-node-js-applications/ 正 ...
- Jmeter 学习(三)
1. 线程组知识 1)Ramp-up period 表示多长时间内建立全部的线程数N 默认为0,表示测试开始即建立全部线程并立即发送访问请求 设置为Ts,表示每隔T/N建立一个线程 注1:一般不设置为 ...
- G将军的敢死队——树状DP
当前节点的两种情况: 1.beChoosed = {son.beAbandoned乘积} //当前节点选中的情况下,子节点都不能选 2.beAbandoned = {(son.beAbandoned ...
- NodeJS 初体验
console.log('%s: %d', 'Hello', 25); // 可以像C语言格式一样输出//app.jsvar http = require('http');http.createSe ...
- Pod(转)
一.CocoaPods的安装 (1)使用淘宝的Ruby镜像替换官方的ruby源,在终端输入命令 $ gem sources --remove https://rubygems.org/ $ gem s ...
- PBOC金融IC卡,卡片与终端交互的13个步骤,简介-第三组
七:终端风险管理-必选但包含可选步骤异常文件:终端检查应用主账号是否在异常文件列表(卡号黑名单)中.商户强制联机:商户可以将当前交易强制为联机处理.最低限额:控制交易当前交易金额或同一张卡片连续几笔交 ...
- windows界面库种类
访问网址http://www.360doc.com/content/14/0612/20/13826502_386093297.shtml
- flume原理及代码实现
转载标明出处:http://www.cnblogs.com/adealjason/p/6240122.html 最近想玩一下流计算,先看了flume的实现原理及源码 源码可以去apache 官网下载 ...
- 【网络】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)
1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...
- Libpci库的调用
这几天发现在Redhat AS6.5 X86_64下用outl(index, 0xcf8)和inl(0xcfc)下读取PCIe配置空间是系统有时性的会hang, 于是去寻找解决方案,首先想到的是用/d ...