File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -40,3 +40,55 @@ const addTwoNumbers = function(l1, l2) {
40
40
41
41
return list . val === 0 ? list . next : list ;
42
42
} ;
43
+
44
+ // another
45
+
46
+ /**
47
+ * Definition for singly-linked list.
48
+ * function ListNode(val, next) {
49
+ * this.val = (val===undefined ? 0 : val)
50
+ * this.next = (next===undefined ? null : next)
51
+ * }
52
+ */
53
+ /**
54
+ * @param {ListNode } l1
55
+ * @param {ListNode } l2
56
+ * @return {ListNode }
57
+ */
58
+ const addTwoNumbers = function ( l1 , l2 ) {
59
+ const s1 = [ ] , s2 = [ ]
60
+ let h1 = l1 , h2 = l2
61
+ while ( h1 ) {
62
+ s1 . push ( h1 . val )
63
+ h1 = h1 . next
64
+ }
65
+ while ( h2 ) {
66
+ s2 . push ( h2 . val )
67
+ h2 = h2 . next
68
+ }
69
+ let inc = false
70
+ let tail = null
71
+ while ( s1 . length || s2 . length ) {
72
+ let tmp = 0
73
+ if ( s1 . length ) tmp += s1 . pop ( )
74
+ if ( s2 . length ) tmp += s2 . pop ( )
75
+ if ( inc ) tmp ++
76
+ if ( tmp > 9 ) {
77
+ inc = true
78
+ } else {
79
+ inc = false
80
+ }
81
+ tmp = tmp % 10
82
+ const cur = new ListNode ( tmp )
83
+ if ( tail ) cur . next = tail
84
+ tail = cur
85
+ }
86
+
87
+ if ( inc ) {
88
+ const head = new ListNode ( 1 )
89
+ head . next = tail
90
+ return head
91
+ }
92
+ return tail
93
+
94
+ } ;
You can’t perform that action at this time.
0 commit comments