Binary Search

July 08, 2019 by Sandeep Bhardwaj | Tags:

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 more

Add two numbers represented as LinkedList

July 07, 2019 by Sandeep Bhardwaj | Tags:

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 more

Detect a loop in LinkedList

May 21, 2018 by Sandeep Bhardwaj | Tags:

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 more

Reverse LinkedList Recursively

May 20, 2018 by Sandeep Bhardwaj | Tags:

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 more

Reverse LinkedList Iteratively

May 19, 2018 by Sandeep Bhardwaj | Tags:

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