July 08, 2019 by Sandeep Bhardwaj | Tags: Leetcode Datastructure & Algorithms
Binary Search Iterative and Recursive /** * 704. Binary Search * <p> * Given a sorted (in ascending order) integer array nums of n elements and a target value, write a * function to search target in nums. If target exists, then return its index, otherwise return -1. * <p> * <...
Read moreJuly 07, 2019 by Sandeep Bhardwaj | Tags: Leetcode Datastructure & Algorithms
Add two numbers represented as LinkedList You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do...
Read moreMay 21, 2018 by Sandeep Bhardwaj | Tags: Datastructure & Algorithms
Detect loop in a linked list. Floyd’s Cycle-Finding Algorithm: Traverse linked list using two pointers. Move one pointer(slowPtr) by one and other pointer(fastPtr) by two. If these pointers meet at some node then there is a loop. If pointers do not meet then linked list doesn’t have loop. Lin...
Read moreMay 20, 2018 by Sandeep Bhardwaj | Tags: Datastructure & Algorithms
Reverse a linked list recursively. LinkedList.java public class LinkedList<E> { static class Node<E> { E data; Node<E> next; Node(E data, Node<E> next) { this.data = data; this.next = next; } }// end of node class private Node<E> head = null; No...
Read moreMay 19, 2018 by Sandeep Bhardwaj | Tags: Datastructure & Algorithms
LinkedList.java public class LinkedList<E> { static class Node<E> { E data; Node<E> next; Node(E data, Node<E> next) { this.data = data; this.next = next; } }// end of node class private Node<E> head = null; Node<E> getHead() { return he...
Read more