Skip to content

Commit 3ab52a0

Browse files
Create 0021_Merge Two Sorted Lists.java
1 parent 1873ea2 commit 3ab52a0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution
2+
{
3+
public ListNode mergeTwoLists(ListNode list1, ListNode list2)
4+
{
5+
if(list1 == null) return list2;
6+
else if(list2 == null) return list1;
7+
ListNode result = new ListNode(0);
8+
ListNode current = result;
9+
while(list1 != null && list2!= null)
10+
{
11+
if(list1.val <= list2.val)
12+
{
13+
current.next = list1;
14+
list1 = list1.next;
15+
}
16+
else
17+
{
18+
current.next = list2;
19+
list2 = list2.next;
20+
}
21+
current = current.next;
22+
}
23+
current.next = list1 == null ? list2:list1;
24+
return result.next;
25+
}
26+
}

0 commit comments

Comments
 (0)