LeetCode Add Two Numbers II
2016-12-26 09:03
330 查看
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/
题目:
You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
题解:
类似Add Two Numbers.
可看成是Add Two Numbers与Reverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.
Time Complexity: O(n). reverse O(n), add O(n).
Space: O(1). reverse O(1), add O(1).
AC Java:
也可以使用两个stack把list 1 和 list 2 分别压进去. 再pop出来相加放到new list的head位置.
Time Complexity: O(n). 压stack用了O(n), pop后相加用了O(n).
Space: O(n). stack用了O(n). result list用了O(n).
AC Java:
题目:
You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7
题解:
类似Add Two Numbers.
可看成是Add Two Numbers与Reverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.
Time Complexity: O(n). reverse O(n), add O(n).
Space: O(1). reverse O(1), add O(1).
AC Java:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 public class Solution { 10 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 11 if(l1 == null){ 12 return l2; 13 } 14 if(l2 == null){ 15 return l1; 16 } 17 l1 = reverseList(l1); 18 l2 = reverseList(l2); 19 20 ListNode cur = l1; 21 int len1 = 0; 22 while(cur != null){ 23 len1++; 24 cur = cur.next; 25 } 26 27 cur = l2; 28 int len2 = 0; 29 while(cur != null){ 30 len2++; 31 cur = cur.next; 32 } 33 34 ListNode dummy = new ListNode(0); 35 if(len1 > len2){ 36 dummy.next = l1; 37 }else{ 38 dummy.next = l2; 39 } 40 cur = dummy; 41 int carry = 0; 42 43 while(l1 != null || l2 != null){ 44 if(l1 != null){ 45 carry += l1.val; 46 l1 = l1.next; 47 } 48 if(l2 != null){ 49 carry += l2.val; 50 l2 = l2.next; 51 } 52 cur.next.val = carry%10; 53 cur = cur.next; 54 carry /= 10; 55 } 56 if(carry != 0){ 57 cur.next = new ListNode(1); 58 } 59 60 ListNode nxt = dummy.next; 61 ListNode newHead = reverseList(nxt); 62 nxt.next = null; 63 return newHead; 64 } 65 private ListNode reverseList(ListNode head){ 66 if(head == null || head.next == null){ 67 return head; 68 } 69 ListNode tail = head; 70 ListNode cur = head; 71 ListNode pre; 72 ListNode temp; 73 while(tail.next != null){ 74 pre = cur; 75 cur = tail.next; 76 temp = cur.next; 77 cur.next = pre; 78 tail.next = temp; 79 } 80 return cur; 81 } 82 }
也可以使用两个stack把list 1 和 list 2 分别压进去. 再pop出来相加放到new list的head位置.
Time Complexity: O(n). 压stack用了O(n), pop后相加用了O(n).
Space: O(n). stack用了O(n). result list用了O(n).
AC Java:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 public class Solution { 10 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 11 if(l1 == null){ 12 return l2; 13 } 14 if(l2 == null){ 15 return l1; 16 } 17 18 Stack<Integer> stk1 = new Stack<Integer>(); 19 Stack<Integer> stk2 = new Stack<Integer>(); 20 while(l1 != null){ 21 stk1.push(l1.val); 22 l1 = l1.next; 23 } 24 while(l2 != null){ 25 stk2.push(l2.val); 26 l2 = l2.next; 27 } 28 29 ListNode dummy = new ListNode(0); 30 int carry = 0; 31 while(!stk1.isEmpty() || !stk2.isEmpty()){ 32 if(!stk1.isEmpty()){ 33 carry += stk1.pop(); 34 } 35 if(!stk2.isEmpty()){ 36 carry += stk2.pop(); 37 } 38 ListNode cur = new ListNode(carry%10); 39 cur.next = dummy.next; 40 dummy.next = cur; 41 carry /= 10; 42 } 43 if(carry != 0){ 44 ListNode cur = new ListNode(1); 45 cur.next = dummy.next; 46 dummy.next = cur; 47 } 48 return dummy.next; 49 } 50 }
相关文章推荐
- Web Form 和asp.net mvc 差别
- MSSQL操作类
- snmp自定义处理程序
- C++之继承与多态
- 正则验证手机号码
- 装饰器模式
- mysql日常运维与参数调优
- MyBatis-使用mybatis-generator-core.jar生成POJO和Mapper文件
- dede常用安全防护
- 一维数组重采样
- 转换机和路由器工作原理
- Android中Listview下拉刷新和上拉加载更多的多种实现方案
- 刷题日记
- [转]优秀程序设计的Kiss原则(keep it simple,stupid)
- js正则表达式惰性匹配和贪婪匹配用法分析
- HttpServletRequest
- mysql权限管理
- Python快速从注释生成文档的方法
- App架构师-》全球android公司/个人排名
- 学生信息排序