-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMerge2LL_GFG.java
More file actions
53 lines (48 loc) · 1.23 KB
/
Merge2LL_GFG.java
File metadata and controls
53 lines (48 loc) · 1.23 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
51
52
53
https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1#
/*
Merge two linked lists
head pointer input could be NULL as well for empty list
Node is defined as
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
*/
class LinkedList
{
//Function to merge two sorted linked list.
Node sortedMerge(Node head1, Node head2) {
Node head3;
if(head1 == null){
return head2;
}
if(head2 == null){
return head1;
}
if(head1.data<= head2.data){
head3 = head1;
head1 = head1.next; // Move to the Next Node
}
else{
head3 = head2;
head2 = head2.next;
}
Node temp = head3;
while(head1!=null && head2!=null){
if(head1.data<=head2.data){
temp.next = head1;
temp = temp.next;
head1 = head1.next;
}
else{
temp.next = head2;
temp = temp.next;
head2 = head2.next;
}
}
temp.next = head1!=null?head1: head2;
return head3;
}
}