Find Middle Element of a Linked List – Java Code

In this tutorial, We are going to explain how to find middle element of a linked list in Java. This programming question is mostly asked in a technical interviews.

For example: Given a linked list, the middle element in this linked list is 8.

15 -> 9 -> 8 -> 5 -> 6 -> NULL

There are multiple approaches to solve this problem. In this tutorial, We are going to learn two approaches through which we can find middle element of a singly linked list. Also, i have also added video tutorial at the end of this tutorial.

Java Program to Reverse a String using Stack

Write a java program to reverse a string using stack data structure. Given an input string, We have to write a java code to reverse a string using stack.

In my previous tutorial, I have explained java program to reverse a string using recursion.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Before solving this problem, Let’s understand what is a stack data structure.

Find Pairs with Given Sum in a Sorted Array – Java Code

Java code to find pairs with given sum in a sorted array. Given an array A of size N. Write a java code to find all pairs in the array that sum to a number equal to K. If no such pair exists then output will be 1.  Array elements are distinct and in a sorted order.

For example –

Input :

arr[] = {1, 2, 3, 4, 5, 6, 7};
sum = 9

Output:

Pairs with given sum 9 is

Pair (2 , 7 )
Pair (3 , 6 )
Pair (4 , 5 )

How to Check if a Number is a Power of 2

How to check if a number is a power of 2.  To understand this question, let’s take some example.

Example –

Input – 16   – 16 is a power of 2 (2^4).

Input – 15 – 15 is not a power of 2.

Input – 32- 32 is a power of 2 (2^5).

We can use multiple approaches to check whether a number is a power of 2 or not.