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. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
其实就是简单的加法。
第一位就是各位,往上累加便可。node
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode result = new ListNode(-1); ListNode p = result; ListNode a = l1; ListNode b = l2; int flag = 0; while (a!=null || b!= null) { int add1 = a == null ? 0 : a.val; int add2 = b == null ? 0 : b.val; int sum = add1 + add2 + flag; flag = sum >= 10? 1:0; int val = sum%10 ; ListNode temp = new ListNode(val); p.next = temp; p = temp; if (a != null) { a = a.next; } if (b != null) { b = b.next; } } if (flag ==1) { p.next = new ListNode(1); } return result.next; } }
时间复杂度:O(n)
空间复杂度:O(1)
耗时:52 msgit
看了下时间少的算法,基本逻辑是一致的。 我运行了一遍 耗时和这差很少。
官网可能因为啥偶然因素致使他的代码毕竟快吧。算法