-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMerge2SortedLists.java
More file actions
50 lines (43 loc) · 1.01 KB
/
Merge2SortedLists.java
File metadata and controls
50 lines (43 loc) · 1.01 KB
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
package LinkedLists;
/**
* Author - archit.s
* Date - 18/10/18
* Time - 12:02 PM
*/
public class Merge2SortedLists {
public ListNode mergeTwoLists(ListNode A, ListNode B) {
ListNode head = null;
ListNode last = null;
while(A!=null && B!=null){
if(A.val <= B.val){
if(head == null){
head = A;
last = head;
}
else{
last.next = A;
last = last.next;
}
A = A.next;
}
else{
if(head == null){
head = B;
last = head;
}
else{
last.next = B;
last = last.next;
}
B = B.next;
}
}
if(A!=null){
last.next = A;
}
else if(B!=null){
last.next = B;
}
return head;
}
}