-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.java
More file actions
39 lines (39 loc) · 983 Bytes
/
21.java
File metadata and controls
39 lines (39 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 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 mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode();
ListNode curr = dummy;
ListNode h1=list1,h2=list2;
while(h1!=null && h2!=null){
if(h1.val<=h2.val){
curr.next=h1;
h1=h1.next;
}
else{
curr.next=h2;
h2=h2.next;
}
curr=curr.next;
}
while(h1!=null){
curr.next=h1;
h1=h1.next;
curr=curr.next;
}
while(h2!=null){
curr.next=h2;
h2=h2.next;
curr=curr.next;
}
return dummy.next;
}
}