|
| 1 | +/** |
| 2 | + * Definition for singly-linked list. |
| 3 | + * struct ListNode { |
| 4 | + * int val; |
| 5 | + * ListNode *next; |
| 6 | + * ListNode(int x) : val(x), next(NULL) {} |
| 7 | + * }; |
| 8 | + */ |
| 9 | +class Solution { |
| 10 | +public: |
| 11 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 12 | + ListNode *result = nullptr; |
| 13 | + ListNode *itr; |
| 14 | + int temp=0; |
| 15 | + int carry =0; |
| 16 | + while(l1 != nullptr && l2 != nullptr) { |
| 17 | + temp = l1->val + l2->val + carry; |
| 18 | + ListNode *ptr = new ListNode(temp%10); |
| 19 | + carry = temp / 10; |
| 20 | + if(result == nullptr) { |
| 21 | + result = ptr; |
| 22 | + itr = result; |
| 23 | + } else { |
| 24 | + itr->next = ptr; |
| 25 | + itr = itr->next; |
| 26 | + } |
| 27 | + l1 = l1->next; |
| 28 | + l2 = l2->next; |
| 29 | + } |
| 30 | + while(l1 != nullptr) { |
| 31 | + temp = l1->val + carry; |
| 32 | + ListNode *ptr = new ListNode(temp%10); |
| 33 | + carry = temp/10; |
| 34 | + if(result == nullptr) { |
| 35 | + result = ptr; |
| 36 | + itr = result; |
| 37 | + } else { |
| 38 | + itr->next = ptr; |
| 39 | + itr = itr->next; |
| 40 | + } |
| 41 | + l1 = l1 -> next; |
| 42 | + } |
| 43 | + while(l2 != nullptr) { |
| 44 | + temp = l2->val + carry; |
| 45 | + ListNode *ptr = new ListNode(temp%10); |
| 46 | + carry = temp/10; |
| 47 | + if(result == nullptr) { |
| 48 | + result = ptr; |
| 49 | + itr = result; |
| 50 | + } else { |
| 51 | + itr->next = ptr; |
| 52 | + itr = itr->next; |
| 53 | + } |
| 54 | + l2 = l2->next; |
| 55 | + } |
| 56 | + if(carry > 0) { |
| 57 | + ListNode *ptr = new ListNode(carry); |
| 58 | + itr->next = ptr; |
| 59 | + itr = itr->next; |
| 60 | + } |
| 61 | + return result; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +void trimLeftTrailingSpaces(string &input) { |
| 66 | + input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) { |
| 67 | + return !isspace(ch); |
| 68 | + })); |
| 69 | +} |
| 70 | + |
| 71 | +void trimRightTrailingSpaces(string &input) { |
| 72 | + input.erase(find_if(input.rbegin(), input.rend(), [](int ch) { |
| 73 | + return !isspace(ch); |
| 74 | + }).base(), input.end()); |
| 75 | +} |
| 76 | + |
| 77 | +vector<int> stringToIntegerVector(string input) { |
| 78 | + vector<int> output; |
| 79 | + trimLeftTrailingSpaces(input); |
| 80 | + trimRightTrailingSpaces(input); |
| 81 | + input = input.substr(1, input.length() - 2); |
| 82 | + stringstream ss; |
| 83 | + ss.str(input); |
| 84 | + string item; |
| 85 | + char delim = ','; |
| 86 | + while (getline(ss, item, delim)) { |
| 87 | + output.push_back(stoi(item)); |
| 88 | + } |
| 89 | + return output; |
| 90 | +} |
| 91 | + |
| 92 | +ListNode* stringToListNode(string input) { |
| 93 | + // Generate list from the input |
| 94 | + vector<int> list = stringToIntegerVector(input); |
| 95 | + |
| 96 | + // Now convert that list into linked list |
| 97 | + ListNode* dummyRoot = new ListNode(0); |
| 98 | + ListNode* ptr = dummyRoot; |
| 99 | + for(int item : list) { |
| 100 | + ptr->next = new ListNode(item); |
| 101 | + ptr = ptr->next; |
| 102 | + } |
| 103 | + ptr = dummyRoot->next; |
| 104 | + delete dummyRoot; |
| 105 | + return ptr; |
| 106 | +} |
| 107 | + |
| 108 | +string listNodeToString(ListNode* node) { |
| 109 | + if (node == nullptr) { |
| 110 | + return "[]"; |
| 111 | + } |
| 112 | + |
| 113 | + string result; |
| 114 | + while (node) { |
| 115 | + result += to_string(node->val) + ", "; |
| 116 | + node = node->next; |
| 117 | + } |
| 118 | + return "[" + result.substr(0, result.length() - 2) + "]"; |
| 119 | +} |
| 120 | + |
| 121 | +int main() { |
| 122 | + string line; |
| 123 | + while (getline(cin, line)) { |
| 124 | + ListNode* l1 = stringToListNode(line); |
| 125 | + getline(cin, line); |
| 126 | + ListNode* l2 = stringToListNode(line); |
| 127 | + |
| 128 | + ListNode* ret = Solution().addTwoNumbers(l1, l2); |
| 129 | + |
| 130 | + string out = listNodeToString(ret); |
| 131 | + cout << out << endl; |
| 132 | + } |
| 133 | + return 0; |
| 134 | +} |
0 commit comments