Detect Loop in a Linked List

How to detect loop in a linked list. Given a linked list, determine if it has a cycle/loop in it.

For example:

If you see in this example, the last node (whose value is 9) instead of pointing to null, points to a node whose value is 5.

Detect a loop in a linked list

Detect loop in a linked list

Binary Tree Level Order Traversal

Given a binary tree, print the level order traversal of its node’s values.

For example:

If you see the below example, the level order traversal of binary tree is 7, 6, 5, 4, 3, 2, 1.

Subscribe Our Tutorials

Get Latest Updates on Facebook

level by level traversal of binary tree

level by level traversal of binary tree

 

Remove Duplicates from an Unsorted Array

Given an unsorted array of integers, write a java code to remove duplicates from an unsorted array.

For example:

Input :{5, 1, 2, 6, 4, 4, 5}
Output :{5, 1, 2, 6, 4}

In this example, 4 and 5 appear multiple times in an input array. After removing the duplicate element from an array we have printed the distinct elements of an array.

Find the Element that Appears once in a Sorted Array

Find the element that appears once in a sorted array where every other element appears twice.

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.

For example:

Example 1:

Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
Output: 2

Except 2 every other element appears twice.

Example 2:

Input: [3, 3, 7, 7, 10, 11, 11]
Output: 10

Except 10 every other element appears twice.

Find Maximum Difference between Two Elements of an Array

Given an array of integers, find Maximum difference between two elements such that larger number appears after the smaller number. In this tutorial, I am going to discuss multiple approaches and their java code to find maximum difference between two elements.

For example :

Example 1:

arr = {2, 5, 15, 6, 4}
Output: 13

The difference between 15 and 2 is 13. Element 15 is greater than 2 and it satisfies our condition that larger number appears after the smaller number.

Example 2:

arr = {7, 9, 5, 6, 13, 2};
Output : 8

The difference between 13 and 5 is 8 (13-5).