Skip to content

Commit

Permalink
Added for 8 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 8, 2024
1 parent 45ab767 commit 949fdd9
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
131 changes: 131 additions & 0 deletions GeeksForGeeks/8-1-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/8-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m+n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/8-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(h)
18 changes: 18 additions & 0 deletions LeetCode/8-1-24/Solution.java
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);
}
}

0 comments on commit 949fdd9

Please sign in to comment.