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

Commit9d051d1

Browse files
authored
Merge pull requestneetcode-gh#594 from notauserx/21-Merge-Two-Sorted-Lists-csharp
Create 21-Merge-Two-Sorted-Lists.cs
2 parents47eb35d +45fcf00 commit9d051d1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
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