-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update solutions to leetcode problem: No.0002
- Loading branch information
Showing
7 changed files
with
214 additions
and
74 deletions.
There are no files selected for viewing
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
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
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
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
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 |
---|---|---|
@@ -1,26 +1,28 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* public int val; | ||
* public ListNode next; | ||
* public ListNode(int val=0, ListNode next=null) { | ||
* this.val = val; | ||
* this.next = next; | ||
* } | ||
* } | ||
*/ | ||
public class Solution { | ||
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { | ||
return AddInternal(l1, l2, false); | ||
} | ||
|
||
private ListNode AddInternal(ListNode l1, ListNode l2, bool plusOne) | ||
{ | ||
if (l1 == null && l2 == null) | ||
ListNode dummy = new ListNode(-1); | ||
ListNode cur = dummy; | ||
var carry = 0; | ||
while (l1 != null || l2 != null || carry != 0) | ||
{ | ||
if (plusOne) | ||
{ | ||
return new ListNode(1); | ||
} | ||
return null; | ||
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry; | ||
carry = t / 10; | ||
cur.next = new ListNode(t % 10); | ||
cur = cur.next; | ||
l1 = l1 == null ? null : l1.next; | ||
l2 = l2 == null ? null : l2.next; | ||
} | ||
|
||
var val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (plusOne ? 1 : 0); | ||
plusOne = val >= 10; | ||
val %= 10; | ||
return new ListNode(val) | ||
{ | ||
//next = AddInternal(l1?.next, l2?.next, plusOne); | ||
next = AddInternal(l1 == null ? null : l1.next, l2 == null ? null : l2.next, plusOne) | ||
}; | ||
return dummy.next; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
/** | ||
* 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 res = new ListNode(-1); | ||
ListNode cur = res; | ||
int quotient = 0; | ||
while (l1 != null || l2 != null || quotient != 0) { | ||
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + quotient; | ||
quotient = t / 10; | ||
ListNode node = new ListNode(t % 10); | ||
cur.next = node; | ||
cur = node; | ||
l1 = (l1 == null) ? l1 : l1.next; | ||
l2 = (l2 == null) ? l2 : l2.next; | ||
int carry = 0; | ||
ListNode dummy = new ListNode(-1); | ||
ListNode cur = dummy; | ||
while (l1 != null || l2 != null || carry != 0) { | ||
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry; | ||
carry = t / 10; | ||
cur.next = new ListNode(t % 10); | ||
cur = cur.next; | ||
l1 = l1 == null ? null : l1.next; | ||
l2 = l2 == null ? null : l2.next; | ||
} | ||
return res.next; | ||
return dummy.next; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,18 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def addTwoNumbers(self, l1, l2): | ||
""" | ||
:type l1: ListNode | ||
:type l2: ListNode | ||
:rtype: ListNode | ||
""" | ||
ans=ListNode(-1) | ||
mn=ans | ||
tmp1=[] | ||
while l1: | ||
tmp1.append(l1.val) | ||
l1=l1.next | ||
tmp1.reverse() | ||
l11='' | ||
for i in tmp1: | ||
l11+=str(i) | ||
l11=int(l11) | ||
tmp2=[] | ||
while l2: | ||
tmp2.append(l2.val) | ||
l2=l2.next | ||
tmp2.reverse() | ||
l22='' | ||
for i in tmp2: | ||
l22+=str(i) | ||
l22=int(l22) | ||
tmp=l11+l22 | ||
tmp=str(tmp) | ||
tmp3=[] | ||
for i in tmp: | ||
tmp3.append(i) | ||
tmp3.reverse() | ||
for j in tmp3: | ||
ans.next=ListNode(int(j)) | ||
ans=ans.next | ||
return mn.next | ||
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
carry = 0 | ||
dummy = ListNode(-1) | ||
cur = dummy | ||
while l1 or l2 or carry: | ||
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry | ||
carry = t // 10 | ||
cur.next = ListNode(t % 10) | ||
cur = cur.next | ||
l1 = None if not l1 else l1.next | ||
l2 = None if not l2 else l2.next | ||
return dummy.next |