File tree Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change @@ -25,35 +25,35 @@ class Solution {
2525
2626public:
2727 ListNode *addTwoNumbers (ListNode *l1, ListNode *l2) {
28- int x=0 , y=0 , z =0 ;
28+ int x=0 , y=0 , carry= 0 , sum =0 ;
2929 ListNode *h=NULL , **t=&h;
3030
3131 while (l1!=NULL || l2!=NULL ){
32- x = getValueAndMoveNext (& l1);
33- y = getValueAndMoveNext (& l2);
32+ x = getValueAndMoveNext (l1);
33+ y = getValueAndMoveNext (l2);
3434
35- z = z + x + y;
35+ sum = carry + x + y;
3636
37- ListNode *node = new ListNode (z %10 );
37+ ListNode *node = new ListNode (sum %10 );
3838 *t = node;
3939 t = (&node->next );
4040
41- z = z /10 ;
41+ carry = sum /10 ;
4242 }
4343
44- if (z > 0 ) {
45- ListNode *node = new ListNode (z %10 );
44+ if (carry > 0 ) {
45+ ListNode *node = new ListNode (carry %10 );
4646 *t = node;
4747 }
4848
4949 return h;
5050 }
5151private:
52- int getValueAndMoveNext (ListNode** l){
52+ int getValueAndMoveNext (ListNode* & l){
5353 int x = 0 ;
54- if ( (*l) != NULL ){
55- x = (*l) ->val ;
56- (*l) = (*l) ->next ;
54+ if (l != NULL ){
55+ x = l ->val ;
56+ l = l ->next ;
5757 }
5858 return x;
5959 }
You can’t perform that action at this time.
0 commit comments