2. Add Two Numbers
来源:互联网 发布:新时代证券交易软件 编辑:程序博客网 时间:2024/05/22 12:58
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(0); ListNode p = head; int flag = 0; int temp = 0; while((l1!=null)||(l2!=null)||(flag==1)){ ListNode newnode =new ListNode(0); temp = flag+((l1!=null)?l1.val:0)+((l2!=null)?l2.val:0); l1 = (l1!=null)?l1.next:l1; l2 = (l2!=null)?l2.next:l2; newnode.val = temp%10; flag = temp/10; p.next = newnode; p = newnode; } return head.next; }}
阅读全文
0 0
- 2.Add Two Numbers
- 2.Add Two Numbers
- 2.Add Two Numbers
- 2.Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2.Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- 2.Add Two Numbers
- 2. Add Two Numbers
- 2. Add Two Numbers
- Git学习笔记
- 自定义View实现点值选择效果(二)
- P1017 进制转换
- iOS第三方库汇总
- 详解ios之ARkit为何碾压对手(一)
- 2. Add Two Numbers
- android studio 环境配置
- VS2013中MSComm通讯控件的配置
- Spark SQL基础学习【三】以json的方式存储
- 51Nod1574(新姿势:使序列有序的最小交换次数)
- CentOS7 安装python3.6 以及多版本python管理
- UVA658[It's not a Bug, it's a Feature!] BellmanFord || Dijkstra 求最短路
- Java表达式计算转型规则
- Java Stream(摘自java8 API文档及stackflow)