coming soon. Please follow and like us:

# Category: DS & Algorithms

Data structure and algorithms in JAVA

## Sorting Algorithms in JAVA

What is sorting? Sorting is an algorithm that arranges the elements of any data structure in a certain order either in increasing order or decreasing order. It is an important topic in computer science and lot of research has gone into this category. If we consider sorting algorithms in computer science then best behavior is […]

## Hashing Data Structure

What is Hashing? Hashing is a technique of getting a data faster whether it is stored in database or any data structure. Hash is value generated from any string , integer or file.It is generally smaller than itself.It is a mathematical concept of generating a hash function.Every time the hash function must generate a different […]

## BackTracking and Recursion

In this tutorial i am going to show the difference between recursion and backtracking . Backtracking is general algorithm for finding solution to some computational problem. We have set of several choices. If one choice from set of choices proves incorrect, computation backtracks or restarts at the point of choice and tries another choice. In […]

## Stacks

Stack is a ADT(Abstract Data Type) used to store elements. In stack order in which data arrives is important. Lets take a simple example. Plates are added to stack as they cleaned. So a Stack is an ordered list which insertions and deletions are done at one end LIFO(last in first out) or FILO(first in last […]

## Linked List Problems

How can you remove duplicates from linked list ?

1 2 3 4 5 6 7 8 9 10 11 12 13 |
synchronized void removeDuplicate(NodeList node) { HashSet<Integer> hashSet = new HashSet<Integer>(); NodeList previous = null; while (node != null) { if (hashSet.contains(node.getData())) { previous.setNext(node.getNext()); } else { hashSet.add(node.getData()); previous = node; } node = node.getNext(); } } |

2nd way

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
synchronized void removeDuplicateWithoutBuffer(NodeList node) { NodeList current = node; while (current != null) { NodeList temp = current; while (temp.getNext() != null) { if (temp.getNext().getData() == current.getData()) { temp.setNext(temp.getNext().getNext()); } else { temp = temp.getNext(); } } current = current.getNext(); } } |

How can you find if List is palindrome or not?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
synchronized void palindromeOrNot(NodeList node, int size) { NodeList head = null; NodeList temp = node; while (temp != null) { NodeList newNode = new NodeList(); newNode.setData(temp.getData()); newNode.setNext(head); head = newNode; temp = temp.getNext(); } equalORNot(node, head); } synchronized void equalORNot(NodeList originalNode, NodeList newNode) { boolean insideLoop = false; while (originalNode != null && newNode != null) { if (originalNode.getData() != newNode.getData()) { System.out.println("oops!! Linked List is not palindrome"); insideLoop = true; break; } originalNode = originalNode.getNext(); newNode = newNode.getNext(); } if (!insideLoop) System.out.println("yes!!! Linked List is palindrome"); } |

How can you reverse a linked list?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
synchronized NodeList reverseList() { NodeList temp = head; NodeList prev = null; NodeList another = null; while (temp != null) { NodeList next = temp.getNext(); temp.setNext(prev); prev = temp; if (temp != null) another = temp; temp = next; } return another; } |

Please follow and like us:

## Linked List

Linked list datastructure which contains data as well as address of next or previous node. There are three types of linked list i.e Singly Linked List Doubly Linked List Circular Linked List Java used doubly linked list which contains address of next and previous node. Insertion is fast in linked list having o(1) time complexity […]

## Datastructure and algorithms in Java

What is Datastructure and algorithm ? Datastructure is the systematic way to organize data, so that it can be retrieved quickly. Different datastructure in programming & database like Stack, Linkedlist , Hashing , Tree , B Tree. Algorithm can be defined so that set of instructions that can solve a particular task in less time. […]