diff --git a/GeeksForGeeks/8-1-24/GFG.java b/GeeksForGeeks/8-1-24/GFG.java new file mode 100644 index 0000000..3cf88e5 --- /dev/null +++ b/GeeksForGeeks/8-1-24/GFG.java @@ -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; + } +} diff --git a/GeeksForGeeks/8-1-24/README.md b/GeeksForGeeks/8-1-24/README.md new file mode 100644 index 0000000..670c95f --- /dev/null +++ b/GeeksForGeeks/8-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(m+n) +Space complexity - O(1) diff --git a/LeetCode/8-1-24/README.md b/LeetCode/8-1-24/README.md new file mode 100644 index 0000000..722dfdc --- /dev/null +++ b/LeetCode/8-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(h) diff --git a/LeetCode/8-1-24/Solution.java b/LeetCode/8-1-24/Solution.java new file mode 100644 index 0000000..9596e4d --- /dev/null +++ b/LeetCode/8-1-24/Solution.java @@ -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); + } +}