File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 353
353
441|[ Arranging Coins] ( ./0441-arranging-coins.js ) |Easy|
354
354
442|[ Find All Duplicates in an Array] ( ./0442-find-all-duplicates-in-an-array.js ) |Medium|
355
355
443|[ String Compression] ( ./0443-string-compression.js ) |Medium|
356
+ 445|[ Add Two Numbers II] ( ./0445-add-two-numbers-ii.js ) |Medium|
356
357
448|[ Find All Numbers Disappeared in an Array] ( ./0448-find-all-numbers-disappeared-in-an-array.js ) |Easy|
357
358
450|[ Delete Node in a BST] ( ./0450-delete-node-in-a-bst.js ) |Medium|
358
359
451|[ Sort Characters By Frequency] ( ./0451-sort-characters-by-frequency.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 445. Add Two Numbers II
3
+ * https://leetcode.com/problems/add-two-numbers-ii/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given two non-empty linked lists representing two non-negative integers. The most
7
+ * significant digit comes first and each of their nodes contains a single digit. Add the two
8
+ * numbers and return the sum as a linked list.
9
+ *
10
+ * You may assume the two numbers do not contain any leading zero, except the number 0 itself.
11
+ */
12
+
13
+ /**
14
+ * Definition for singly-linked list.
15
+ * function ListNode(val, next) {
16
+ * this.val = (val===undefined ? 0 : val)
17
+ * this.next = (next===undefined ? null : next)
18
+ * }
19
+ */
20
+ /**
21
+ * @param {ListNode } l1
22
+ * @param {ListNode } l2
23
+ * @return {ListNode }
24
+ */
25
+ var addTwoNumbers = function ( l1 , l2 ) {
26
+ const s1 = [ ] ;
27
+ const s2 = [ ] ;
28
+ let carry = 0 ;
29
+ let head = null ;
30
+
31
+ while ( l1 ) {
32
+ s1 . push ( l1 . val ) ;
33
+ l1 = l1 . next ;
34
+ }
35
+
36
+ while ( l2 ) {
37
+ s2 . push ( l2 . val ) ;
38
+ l2 = l2 . next ;
39
+ }
40
+
41
+ while ( s1 . length || s2 . length || carry ) {
42
+ const sum = ( s1 . pop ( ) || 0 ) + ( s2 . pop ( ) || 0 ) + carry ;
43
+ carry = sum / 10 | 0 ;
44
+ head = Object . assign ( new ListNode ( sum % 10 ) , { next : head } ) ;
45
+ }
46
+
47
+ return head ;
48
+ } ;
You can’t perform that action at this time.
0 commit comments