-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-4.java
74 lines (59 loc) · 1.46 KB
/
Problem-4.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Merge two sorted Linked list
Problem Link :- https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1
*/
class LinkedList
{
//Function to merge two sorted linked list.
Node sortedMerge(Node head1, Node head2) {
// This is a "method-only" submission.
// You only need to complete this method
Node node1=head1;
Node node2=head2;
Node fin=null;
Node result=fin;
while(node1!=null && node2!=null)
{
if(node1.data<node2.data)
{
if(fin==null)
{
fin=node1;
result=node1;
}
else
{
fin.next=node1;
fin=fin.next;
}
node1=node1.next;
}
else
{
if(fin==null)
{
fin=node2;
result=node2;
}
else
{
fin.next=node2;
fin=fin.next;
}
node2=node2.next;
}
}
if(node1!=null)
{
fin.next=node1;
}
if(node2!=null)
{
fin.next=node2;
}
return result;
}
}