Add Two Numbers-LeetCode
来源:互联网 发布:mini ninjas for mac 编辑:程序博客网 时间:2024/05/21 17:08
更一发:最下方附上了比较精简的大神代码!
题目本意:单向链表模拟大数相加
分析:链表结构已经给定了,需要自己完成大数链表的构造(PS:从头部到尾部存放的依次是从个位到高位的数据),然后按照加法法则去做就可以了。
思路:如果完全按照加法法则去做的话,逻辑会比较复杂,因为每一位计算完毕都要判断是否进位,是否有哪个链表已经为空需要退出计算。所以讨巧点的思路就是借鉴归并排序的做法,现将两个链表每一位相加,保存在新链表里。随后在扫描一遍看是否有进位的地方,若要进位而高位为空的话,就再添加一位存储空间即可。这么做下来,效率是O(2n)。
第一次RUN出错的原因:在指针偏移过程中用到了头指针常量,如果用修饰符保护一下,后面就不会出错去修改了。(Java里没有指针这个概念,都是引用,所以其实我是从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
Subscribe to see which companies asked this question
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode result=null,tr=null,t1=l1,t2=l2;int yvshu=0;if(l1!=null&&l2!=null){result=tr=new ListNode(t1.val+t2.val);tr.next=null;t1=t1.next;t2=t2.next;while(true){if(t1==null){tr.next=t2;break;}else if(t2==null){tr.next=t1;break;}else{tr.next=new ListNode(t1.val+t2.val);tr=tr.next;tr.next=null;t1=t1.next;t2=t2.next;}}tr=result;while(tr!=null){if(tr.val>=10){yvshu=tr.val/10;tr.val=tr.val%10;if(tr.next!=null){tr.next.val+=yvshu;}else{tr.next=new ListNode(yvshu);tr.next.next=null;}}else yvshu=0;tr=tr.next;}}if(l1==null && l2!=null)result = l2;if(l1!=null && l2==null)result = l1;if(l1==null && l2==null)result = null;return result;}}
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode ln1 = l1, ln2 = l2, head = null, node = null; int carry = 0, remainder = 0, sum = 0; head = node = new ListNode(0); while(ln1 != null || ln2 != null || carry != 0) { sum = (ln1 != null ? ln1.val : 0) + (ln2 != null ? ln2.val : 0) + carry; carry = sum / 10; remainder = sum % 10; node = node.next = new ListNode(remainder); ln1 = (ln1 != null ? ln1.next : null); ln2 = (ln2 != null ? ln2.next : null); } return head.next; }}
0 0
- LeetCode: Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- Leetcode : Add Two Numbers
- 【leetcode】Add Two Numbers
- [LeetCode]Add Two Numbers
- leetcode Add Two Numbers
- LeetCode-Add Two Numbers
- [leetcode] Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode - Add Two Numbers
- LeetCode | Add Two Numbers
- 【leetcode】Add Two Numbers
- LinearLayout添加分隔线
- UIViewController Push & Pop 的那些坑
- java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWi
- bzoj 1188
- Centos 6.4 python 2.6 升级到 2.7
- Add Two Numbers-LeetCode
- (模板)区间修改与查询
- 重新设置MySQL的root密码
- C#缓存介绍 Caching通用帮助类代码整理 .net几种缓存概述
- java Data、String、Long三种日期类型之间的相互转换
- 安卓通知控件SnackBar解析
- Android studio问题汇总
- 51nod 1608 管道安装
- libevent