Directory
- 2_ two number added
- Describe
- Method One: Elementary School mathematics
- Ideas
- Java Code (non-recursive notation)
- Java code (recursive notation)
- Python Code (non-recursive notation)
2_ Two number addition description
A two non-empty list is given to represent two non-negative integers. The digits are stored in reverse order, with each node storing only a single number. Adds two numbers to return a new linked list.
You can assume that except for the number 0, none of the two numbers will start with 0.
Example:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807
Method One: Elementary school mathematics thought
According to the sum of two numbers in elementary school mathematics, starting with the lowest bit (linked list header), the carry
result of the carry is saved with the variable (the initial value is 0), and carry
the value of the variable is updated after each sum.
Java Code (non-recursive notation)
/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode N Ext * ListNode (int x) {val = x;} *} */class Solution {public ListNode addtwonumbers (listnode L1, ListNode L2) { ListNode dummyhead = new ListNode (-1); ListNode curnode = dummyhead, p = L1, q = L2; int carry = 0; while (P! = NULL | | Q! = NULL) {int x = (P! = null)? p.val:0; int y = (q! = null)? q.val:0; int sum = x + y + carry; carry = SUM/10; Curnode.next = new ListNode (sum% 10); Curnode = Curnode.next; if (P! = null) {p = p.next; } if (q! = null) {q = Q.next; }} if (Carry > 0) {curnode.next = new ListNode (carry); } return dummyhead.next; }}//runtime:38 ms//Your Runtime beats 46.06% of Java submissions.
Analysis of Complexity:
- Time complexity:\ (O (max (m, n) \), where \ (m\) and \ (n\) represent the length of the two linked lists, respectively.
- Spatial complexity:\ (O (max (m, n) \), returns the list length to \ (Max (m, N) + 1\).
Java code (recursive notation)
/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode N Ext * ListNode (int x) {val = x;} *} */class Solution {public ListNode addtwonumbers (listnode L1, ListNode L2) { Return AddTwoNumbers (L1, L2, 0); } private ListNode AddTwoNumbers (listnode L1, ListNode L2, int carry) {//Recursive termination condition if (L1 = = NULL && L2 = = null) {return carry > 0? new ListNode (carry): null; } int sum = carry; ListNode l1next = null, L2next = NULL; if (L1! = null) {sum + = L1.val; L1next = L1.next; } if (L2! = null) {sum + = L2.val; L2next = L2.next; } ListNode Curr = new ListNode (sum% 10); Curr.next = AddTwoNumbers (L1next, L2next, SUM/10); return curr; }}//runtime:27 ms//Your Runtime beats 94.02% of Java submissions.
The complexity of the analysis.
Python Code (non-recursive notation)
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummy_head = ListNode(-1) cur = dummy_head carry = 0 while l1 or l2 or carry: v1 = v2 = 0 if l1: v1 = l1.val l1 = l1.next if l2: v2 = l2.val l2 = l2.next carry, val = divmod(v1 + v2 + carry, 10) cur.next = ListNode(val) cur = cur.next return dummy_head.next # Runtime: 156 ms# Your runtime beats 43.08 % of python3 submissions.
The complexity of the analysis.
2_ two number added