-
Notifications
You must be signed in to change notification settings - Fork 1
/
21_Merge Two Sorted Lists_2.java
57 lines (53 loc) · 1.13 KB
/
21_Merge Two Sorted Lists_2.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 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; }
* }
*/
/**
* @author WasitShafi
* @since 19-SEP-2020
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else {
ListNode l3 = null;
ListNode cur1 = l1;
ListNode cur2 = l2;
ListNode cur3 = null;
if (cur1.val < cur2.val) {
l3 = l1;
cur3 = l3;
cur1 = cur1.next;
} else {
l3 = l2;
cur3 = l3;
cur2 = cur2.next;
}
while (cur1 != null && cur2 != null) {
if (cur1.val < cur2.val) {
cur3.next = cur1;
cur1 = cur1.next;
} else {
cur3.next = cur2;
cur2 = cur2.next;
}
cur3 = cur3.next;
}
if (cur1 != null) {
cur3.next = cur1;
} else {
cur3.next = cur2;
}
return l3;
}
}
}