random addition

Run Settings
LanguageC
Language Version
Run Command
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ typedef struct ListNode node; void reverse(node** ptr){ node *temp ,*p,*n; temp = *ptr; p = NULL; while (temp != NULL) { n = temp->next ; temp->next = p; p = temp; temp = n; } *ptr = p; } node* addTwoNumbers(node* l1, node* l2){ node *head = NULL; reverse(&l1); reverse(&l2); while(l1 || l2){ int carry = 0; if(l1){ node *temp = (node*)malloc(sizeof(node)) ; // temp->val = (l1->val + l2->val + carry) < 10 ? (l1->val + l2->val + carry) : (l1->val + l2->val + carry)/10 ; int ran = l1->val + l2->val; temp->val = (ran + carry) <= 10 ? (ran + carry)%10 : (ran + carry)/10 ; carry = (ran + carry) >= 10 ? ( (ran + carry) == 10)?1:(ran+ carry)%10 : 0 ; printf("%d %d \n",temp->val,carry ); } if (l1->next) { l1 = l1->next; } if (l2->next){ l2 = l2->next; } if(!l1->next && !l2->next) break ; } return head; }
Editor Settings
Theme
Key bindings
Full width
Lines