LeetCode Problem 2

// given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit. 
//
// If we add up these two numbers, it will return a new list and to represent them.
//
// You can assume that in addition to the numbers 0, these two numbers will not begin with 0.
//
// Example:
//
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8
@ reasons: 342 + 465 = 807
// source: stay button (LeetCode)
// Links: HTTPS: //leetcode-cn.com/problems/add-two-numbers
class Solution {
public ListNode addTwoNumbers (ListNode L1, L2 ListNode) {
ListNode cur1 = L1;
CUR2 = L2 ListNode;
int In Flag = 0;
int Result = 0;
ListNode new new head ListNode = (0);
ListNode pre = head;
ListNode CUR = head;
while (cur1 != null && cur2 != null) {
result = (cur1.val + cur2.val + flag) % 10;
flag = (cur1.val + cur2.val + flag) / 10;
pre = cur;
cur = new ListNode(result);
pre.next = cur;
cur1 = cur1.next;
cur2 = cur2.next;
}

while (cur1 != null) {
result = (cur1.val + flag) % 10;
flag = (cur1.val + flag) / 10;
pre = cur;
cur = new ListNode(result);
pre.next = cur;
cur1 = cur1.next;
}

while (cur2 != null) {
= Result (cur2.val In Flag +) 10%;
In Flag = (cur2.val In Flag +) / 10;
pre = CUR;
CUR = new new ListNode (Result);
pre.next = CUR;
CUR2 = cur2.next;
}

IF (Flag = 0!) {
the Result = Flag;
pre = CUR;
CUR = new new ListNode (the Result);
pre.next = CUR;
}
return head.next;
}

// test code, submit your answers need to delete
public static void main ( String [] args) {
Solution Solution Solution new new = ();
ListNode ListNode new new L1 = (2);
ListNode new new ListNode L2 = (2);
ListNode ret = solution.addTwoNumbers(l1, l2);
System.out.println("");
}
}

Guess you like

Origin www.cnblogs.com/mockedMonk/p/11223068.html