-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 302 - Leetcode - 0002 Add Two Numbers Changed directory name from 301 to 301 - Witch Jobsite
- Loading branch information
1 parent
dfa5056
commit f0925ed
Showing
15 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { | ||
|
||
ListNode prePreFirstResult = new ListNode(); | ||
prePreFirstResult.next = new ListNode(); | ||
ListNode result = prePreFirstResult.next; | ||
int carry = 0; | ||
// While a new node is necessary... | ||
while (carry == 1 || l1 != null || l2 != null){ | ||
if (l1 == null){ | ||
l1 = new ListNode(0); | ||
} | ||
if (l2 == null){ | ||
l2 = new ListNode(0); | ||
} | ||
ListNode nextResult = new ListNode ((l1.val + l2.val + carry)%10); | ||
result.next = nextResult; | ||
if (l1.val + l2.val + carry >= 10){ | ||
carry = 1; | ||
} else { | ||
carry = 0; | ||
} | ||
l1 = l1.next; | ||
l2 = l2.next; | ||
result = nextResult; | ||
} | ||
return prePreFirstResult.next.next; | ||
} | ||
} |