-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45ab767
commit 949fdd9
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
class Node | ||
{ | ||
int data; | ||
Node next; | ||
|
||
Node(int d) | ||
{ | ||
data = d; | ||
next = null; | ||
} | ||
} | ||
|
||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int t=scanner.nextInt(); | ||
while(t-->0) | ||
{ | ||
int N = scanner.nextInt(); | ||
int M = scanner.nextInt(); | ||
|
||
Node node1 = null; | ||
Node temp1 = null; | ||
for (int i = 0; i < N; i++) { | ||
int value = scanner.nextInt(); | ||
Node newNode = new Node(value); | ||
if (node1 == null) { | ||
node1 = newNode; | ||
temp1 = node1; | ||
} else { | ||
temp1.next = newNode; | ||
temp1 = temp1.next; | ||
} | ||
} | ||
|
||
Node node2 = null; | ||
Node temp2 = null; | ||
for (int i = 0; i < M; i++) { | ||
int value = scanner.nextInt(); | ||
Node newNode = new Node(value); | ||
if (node2 == null) { | ||
node2 = newNode; | ||
temp2 = node2; | ||
} else { | ||
temp2.next = newNode; | ||
temp2 = temp2.next; | ||
} | ||
} | ||
|
||
GfG gfg = new GfG(); | ||
Node result = gfg.mergeResult(node1, node2); | ||
|
||
printList(result); | ||
} | ||
} | ||
|
||
static void printList(Node node) { | ||
while (node != null) { | ||
System.out.print(node.data + " "); | ||
node = node.next; | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
/* Structure of the node*/ | ||
/* class Node | ||
{ | ||
int data; | ||
Node next; | ||
Node(int d) | ||
{ | ||
data = d; | ||
next = null; | ||
} | ||
} */ | ||
|
||
class GfG | ||
{ | ||
Node mergeResult(Node a, Node b) | ||
{ | ||
// Your code here | ||
Node head = null; // head of the merged list | ||
Node tail = null; // tail of the merged list | ||
|
||
while (a != null && b != null) | ||
{ | ||
if (a.data < b.data) | ||
{ | ||
Node temp = a.next; | ||
a.next = head; | ||
head = a; | ||
a = temp; | ||
} | ||
else | ||
{ | ||
Node temp = b.next; | ||
b.next = head; | ||
head = b; | ||
b = temp; | ||
} | ||
} | ||
|
||
while (a != null) | ||
{ | ||
Node temp = a.next; | ||
a.next = head; | ||
head = a; | ||
a = temp; | ||
} | ||
|
||
while (b != null) | ||
{ | ||
Node temp = b.next; | ||
b.next = head; | ||
head = b; | ||
b = temp; | ||
} | ||
|
||
return head; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(m+n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution | ||
{ | ||
public int rangeSumBST(TreeNode root, int low, int high) | ||
{ | ||
if (root == null) | ||
return 0; | ||
|
||
else if (root.val < low) | ||
return rangeSumBST(root.right, low, high); | ||
|
||
else if (root.val > high) | ||
return rangeSumBST(root.left, low, high); | ||
|
||
return root.val | ||
+ rangeSumBST(root.left, low, high) | ||
+ rangeSumBST(root.right, low, high); | ||
} | ||
} |