Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitddb4017

Browse files
authored
Added task 2
1 parente9dd524 commitddb4017

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespaceLeetCodeNet.Com_github_leetcode{
2+
3+
publicclassLinkedListUtils{
4+
publicstaticListNodeContructLinkedList(int[]nums){
5+
if(nums==null||nums.Length==0){
6+
thrownewArgumentException(
7+
"Please pass in a valid listValues to create a linked list.");
8+
}
9+
ListNodepre=newListNode(-1);
10+
ListNodehead=newListNode(nums[0]);
11+
pre.next=head;
12+
for(inti=1;i<nums.Length;i++){
13+
head.next=newListNode(nums[i]);
14+
head=head.next;
15+
}
16+
returnpre.next;
17+
}
18+
19+
publicstaticListNodeCreateSinglyLinkedList(List<int>listValues){
20+
if(listValues==null||listValues.Count==0){
21+
thrownewArgumentException(
22+
"Please pass in a valid listValues to create a singly linked list.");
23+
}
24+
ListNodehead=newListNode(listValues[0]);
25+
ListNodetmp=head;
26+
for(inti=1;i<listValues.Count;i++){
27+
ListNodenext=newListNode(listValues[i]);
28+
tmp.next=next;
29+
tmp=tmp.next;
30+
}
31+
returnhead;
32+
}
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespaceLeetCodeNet.G0001_0100.S0002_add_two_numbers{
2+
3+
usingXunit;
4+
usingLeetCodeNet.Com_github_leetcode;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidAddTwoNumbers(){
9+
ListNodelistNode1=LinkedListUtils.ContructLinkedList(newint[]{2,4,3});
10+
ListNodelistNode2=LinkedListUtils.ContructLinkedList(newint[]{5,6,4});
11+
Assert.Equal("7, 0, 8",newSolution().AddTwoNumbers(listNode1,listNode2).ToString());
12+
}
13+
14+
[Fact]
15+
publicvoidAddTwoNumbers2(){
16+
Assert.Equal("0",newSolution().AddTwoNumbers(newListNode(0),newListNode(0)).ToString());
17+
}
18+
19+
[Fact]
20+
publicvoidAddTwoNumbers3(){
21+
ListNodelistNode1=LinkedListUtils.ContructLinkedList(newint[]{9,9,9,9,9,9,9});
22+
ListNodelistNode2=LinkedListUtils.ContructLinkedList(newint[]{9,9,9,9});
23+
Assert.Equal("8, 9, 9, 9, 0, 0, 0, 1",newSolution().AddTwoNumbers(listNode1,listNode2).ToString());
24+
}
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespaceLeetCodeNet.Com_github_leetcode{
2+
3+
usingSystem.Text;
4+
5+
publicclassListNode{
6+
publicintval;
7+
publicListNode?next;
8+
9+
publicListNode(){}
10+
11+
publicListNode(intval){
12+
this.val=val;
13+
}
14+
15+
publicListNode(intval,ListNodenext){
16+
this.val=val;
17+
this.next=next;
18+
}
19+
20+
publicoverridestringToString(){
21+
StringBuilderresult=newStringBuilder(val.ToString());
22+
if(next!=null){
23+
ListNodecurrent=next;
24+
while(current.next!=null){
25+
result.Append(", ");
26+
result.Append(current.val);
27+
current=current.next;
28+
}
29+
result.Append(", ");
30+
result.Append(current.val);
31+
}
32+
returnresult.ToString();
33+
}
34+
}
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespaceLeetCodeNet.G0001_0100.S0002_add_two_numbers{
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
4+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
5+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2023_12_19_Time_84_ms_(77.30%)_Space_49.4_MB_(51.01%)
6+
7+
usingLeetCodeNet.Com_github_leetcode;
8+
9+
publicclassSolution{
10+
publicListNodeAddTwoNumbers(ListNodel1,ListNodel2){
11+
ListNodedummyHead=newListNode(0);
12+
ListNodecurr=dummyHead;
13+
intcarry=0;
14+
while(l1!=null||l2!=null){
15+
intx=(l1!=null)?l1.val:0;
16+
inty=(l2!=null)?l2.val:0;
17+
intsum=carry+x+y;
18+
carry=sum/10;
19+
curr.next=newListNode(sum%10);
20+
curr=curr.next;
21+
if(l1!=null)l1=l1.next;
22+
if(l2!=null)l2=l2.next;
23+
}
24+
if(carry>0){
25+
curr.next=newListNode(carry);
26+
}
27+
returndummyHead.next;
28+
}
29+
}
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two**non-empty** linked lists representing two non-negative integers. The digits are stored in**reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 =[2,4,3], l2 =[5,6,4]
14+
15+
**Output:**[7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 =[0], l2 =[0]
22+
23+
**Output:**[0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 =[9,9,9,9,9,9,9], l2 =[9,9,9,9]
28+
29+
**Output:**[8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range`[1, 100]`.
34+
*`0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp