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

Commit45fcf00

Browse files
committed
Create 21-Merge-Two-Sorted-Lists.cs
1 parentbd40ef5 commit45fcf00

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎csharp/21-Merge-Two-Sorted-Lists.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
publicclassSolution{
13+
publicListNodeMergeTwoLists(ListNodelist1,ListNodelist2){
14+
vardummy=newListNode();
15+
vartail=dummy;
16+
17+
while(list1is notnull&&list2is notnull){
18+
if(list1.val<list2.val){
19+
tail.next=list1;
20+
list1=list1.next;
21+
}
22+
else{
23+
tail.next=list2;
24+
list2=list2.next;
25+
}
26+
tail=tail.next;
27+
}
28+
29+
if(list1is notnull)
30+
tail.next=list1;
31+
elseif(list2is notnull)
32+
tail.next=list2;
33+
34+
returndummy.next;
35+
}
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp