-
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
49aaa38
commit eeec2c3
Showing
4 changed files
with
140 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,85 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
class Node{ | ||
int data; | ||
Node next; | ||
|
||
Node(int x){ | ||
data = x; | ||
next = null; | ||
} | ||
|
||
} | ||
class GFG{ | ||
static void printList(Node node) | ||
{ | ||
while (node != null) | ||
{ | ||
System.out.print(node.data + " "); | ||
node = node.next; | ||
} | ||
System.out.println(); | ||
} | ||
public static void main(String args[]) throws IOException { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t > 0){ | ||
int n = sc.nextInt(); | ||
Node head = new Node(sc.nextInt()); | ||
Node tail = head; | ||
for(int i=0; i<n-1; i++) | ||
{ | ||
tail.next = new Node(sc.nextInt()); | ||
tail = tail.next; | ||
} | ||
Solution g = new Solution(); | ||
head = g.deleteMid(head); | ||
printList(head); | ||
t--; | ||
} | ||
} | ||
} | ||
|
||
|
||
// } Driver Code Ends | ||
|
||
|
||
/* | ||
class Node { | ||
int data; | ||
Node next; | ||
Node(int d) { data = d; next = null; } | ||
} | ||
*/ | ||
|
||
class Solution | ||
{ | ||
Node deleteMid(Node head) | ||
{ | ||
// This is method only submission. | ||
// You only need to complete the method. | ||
if (head == null) | ||
return null; | ||
if (head.next == null) | ||
return null; | ||
|
||
Node slow = head; | ||
Node fast = head; | ||
|
||
Node prev = null; | ||
|
||
while (fast != null && fast.next != null) | ||
{ | ||
fast = fast.next.next; | ||
prev = slow; | ||
slow = slow.next; | ||
} | ||
|
||
// Delete the middle node | ||
prev.next = slow.next; | ||
|
||
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(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(n) |
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,51 @@ | ||
class Solution | ||
{ | ||
public int[] sumOfDistancesInTree(int n, int[][] edges) | ||
{ | ||
int[] ans = new int[n]; | ||
int[] count = new int[n]; | ||
Set<Integer>[] tree = new Set[n]; | ||
|
||
Arrays.fill(count, 1); | ||
|
||
for (int i = 0; i < n; ++i) | ||
tree[i] = new HashSet<>(); | ||
|
||
for (int[] edge : edges) | ||
{ | ||
int u = edge[0]; | ||
int v = edge[1]; | ||
tree[u].add(v); | ||
tree[v].add(u); | ||
} | ||
|
||
postorder(tree, 0, -1, count, ans); | ||
preorder(tree, 0, -1, count, ans); | ||
return ans; | ||
} | ||
|
||
private void postorder(Set<Integer>[] tree, int node, int parent, int[] count, int[] ans) | ||
{ | ||
for (int child : tree[node]) | ||
{ | ||
if (child == parent) | ||
continue; | ||
|
||
postorder(tree, child, node, count, ans); | ||
count[node] += count[child]; | ||
ans[node] += ans[child] + count[child]; | ||
} | ||
} | ||
|
||
private void preorder(Set<Integer>[] tree, int node, int parent, int[] count, int[] ans) | ||
{ | ||
for (int child : tree[node]) | ||
{ | ||
if (child == parent) | ||
continue; | ||
|
||
ans[child] = ans[node] - count[child] + (tree.length - count[child]); | ||
preorder(tree, child, node, count, ans); | ||
} | ||
} | ||
} |