- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
- boolean over = false;
- ListNode headNode = new ListNode();
- ListNode currentNode = headNode;
- boolean carryOver = false;
- while(!over) {
- int sum = 0;
- if(l1 == null && l2 == null && !carryOver) {
- over = true;
- }
- else {
- if(carryOver) {
- sum++;
- carryOver = false;
- }
- if(l1 != null) {
- sum += l1.val;
- l1 = (l1.next == null) ? null : l1.next;
- }
- if(l2 != null) {
- sum += l2.val;
- l2 = (l2.next == null) ? null : l2.next;
- }
- if(sum >= 10) {
- carryOver = true;
- sum -= 10;
- }
- currentNode.val = sum;
- currentNode.next = new ListNode();
- currentNode = currentNode.next;
- }
- }
- return headNode;
- }
- };
[text] lmao
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: