File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ const LinkedList = require ( './linkedlist' ) ;
2+
3+ function sumLists ( list1 , list2 , isForwardOrder = false ) {
4+ const result = new LinkedList ( ) ;
5+ let reminder = 0 ;
6+
7+ // TODO: assert list length are the same
8+
9+ for ( let l1 = list1 . head , l2 = list2 . head ; l1 && l2 ; l1 = l1 . next , l2 = l2 . next ) {
10+ // TODO: validate data from both list are numbers
11+ const value = l1 . data + l2 . data + reminder ;
12+ reminder = parseInt ( value / 10 ) ;
13+ result . addLast ( value % 10 ) ;
14+ }
15+ return result ;
16+ }
17+
18+ function test ( ) {
19+ const list1 = new LinkedList ( ) ;
20+ list1 . add ( 6 ) ;
21+ list1 . add ( 1 ) ;
22+ list1 . add ( 7 ) ;
23+
24+
25+ const list2 = new LinkedList ( ) ;
26+ list2 . add ( 2 ) ;
27+ list2 . add ( 9 ) ;
28+ list2 . add ( 5 ) ;
29+
30+
31+ console . log ( list1 . toString ( ) ) ;
32+ console . log ( list2 . toString ( ) ) ;
33+ console . log ( sumLists ( list1 , list2 ) . toString ( ) ) ;
34+ }
35+
36+ test ( ) ;
You can’t perform that action at this time.
0 commit comments