Tuesday 10 October 2017

Reversing a LinkedList using Stack in Java

Hello people, this is another blog dedicated to reversing a LinkedList using a Stack. Although it can be done in several ways like recursion, iteration, using constant auxiliary space, etc, 'Stack' is a very easy and efficient method to do the same.

This has been demonstrated in the code.


import java.util.*;

class TestClass {

static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}

static Node head;
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < 10; i++) {
Node temp = new Node(i);
if(head == null) {
head = temp;
} else {
Node p = head;
while(p.next != null) {
p = p.next;
}
p.next = temp;
}
}
System.out.print("Printing the LinkedList: ");
Node x = head;
while(x != null) {
System.out.print(x.data + " ");
x = x.next;
}
System.out.println();

try {
Node p = head;
while(p != null) {
stack.push(p.data);
p = p.next;
}
System.out.print("Reversing the LinkedList using stack: ");
while(!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println();
} catch (EmptyStackException exception) {
System.out.println("The stack is Empty!");
}
}
}

People, please let me know if this could be improvised and let me know the concerns if any!

Sunday 8 October 2017

Hours, Minutes and Seconds!!!

Hey people, I am having a doubt here, a puerile one!

There is a clock that is 24 hours format and every other property of the clock is as same as the other regular 12 hours format wall clock.

So I am not able to fix this formula to calculate the "Angle between the hour hand, the minute hand and the second hand".

Please look at the code and someone help me fix this.

import java.util.Scanner;

class Solution {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
int testCases = scanner.nextInt();
String[] time1 = new String[3];
String[] time2 = new String[3];
String[] input = new String[testCases];
for(int i = 0; i < testCases; i++) {
input[i] = scanner.next();
}
for(int i = 1; i < testCases; i++) {
time1 = input[i-1].split(":");
time2 = input[i].split(":");
String res = String.format("%.6f %.6f %.6f",
(Math.abs(Float.parseFloat(time2[0]) - Float.parseFloat(time1[0])) * 6),
(Math.abs(Float.parseFloat(time2[1]) - Float.parseFloat(time1[1])) * 6),
(Math.abs(Float.parseFloat(time2[2]) - Float.parseFloat(time1[2])) * 6)
);
System.out.println(res);
}
} catch (Exception exception){
exception.printStackTrace();
} finally {
scanner.close();
}
}
}


The output of the program should be the angle between the hour hand, minute hand and the second hand with respect to the one succeeding it.

For example, consider...

2
10:00:00
12:00:00

is given as the input. In this case, since we can simply calculate that the hour hand has traversed 30 degrees and the hour and minute hand has traversed 0 degree and 0 degree respectively. The output would be

30.000000 0.000000 0.000000

Please help me make a formula for it!

In  case of further clarification, please feel free to clarify!

Thanks a lot in advance! :)

Saturday 7 October 2017

Finding the middle Node of the Linked List using fast and slow method

Fast and slow pointers are very useful in LinkedList especially when traversal of the LinkedList is required several times.

Here is an implementation of fast and slow method for finding the middle of the LinkedList without traversing the whole array.

class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}

class LinkedList {
static Node head;

public static Node printMiddle(Node head) {
Node fast_ptr = head;
Node slw_ptr = head;
if(head != null) {
while(fast_ptr != null && fast_ptr.next != null) {
fast_ptr = fast_ptr.next.next;
slw_ptr = slw_ptr.next;
}
System.out.println("The middle element is: " + slw_ptr.data);
}
return slw_ptr;
}

public static void main(String[] args) {
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.next = new Node(7);
head.next.next.next.next.next.next.next = new Node(8);
head.next.next.next.next.next.next.next.next = new Node(9);

printMiddle(head);
}


Please feel free to comment over for any suggestions / doubts.