__1). Remove K Digits__Given a non-negative integer number represented as a string, remove k digits from the number so that the new number is the smallest possible.

**Note:**

The length of num is less than 10002 and will be ≥ k.

The given num does not contain any leading zero

__Example 1__: Input: num = "1432219", k = 3

Output: "1219"

Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.

__Example 2__: Input: num = "10200", k = 1

Output: "200"

Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.

__Example 3__: Input: num = "10", k = 2

Output: "0"

Explanation: Remove all the digits from the number and it is left with nothing which is 0.

**:**

__Steps__- We will iterate for k times.
- In each iteration we will check remove the current digit if it's equal or smaller then next number.
- After iteration, we need to remove all the leading zeros.

**GIT URL: Java Solution Of Leet Code's Remove K Digits problem**

**Java Solution**

__2). Reverse Linked List__Reverse a linked list from position m to n. Do it in one-pass.

**Note**:

- 1 is less than or equal to m.
- m is less than or equal to n.
- n is less than or equal to the length of list.

**:**

__Example__Input: 1- > 2- > 3- > 4- > 5- > NULL, m = 2, n = 4

Output: 1- > 4- > 3- > 2- > 5- > NULL

**:-**

__Algorithm__Step 1: Decrement m and n until m reaches 1. After completion of step 1:

a). m will become 1 and n will be equal to previous value of m.

b). current will point to the element at n position.

c). previous will be the previous of element at n position.

Step 2: Reverse the nodes until n becomes 0. After completion of step 2:

a). m will remain 1, and n will be 0.

b). current will point to the element at last position.

c). previous will be the previous of element at the last position.

d). previous will be the element at n position (n which was passed).

e). ncurrent will be the element at m position (m which was passed), nprevious will be its previous element.

Step 3: nprevious will point to previous and ncurrent will point to current.

**GIT URL: Leet Code solution: Reverse a linked list from position m to n**

__Java Solution__

__3). Swap Nodes in Pairs__Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed.

**Example**: Given 1- > 2- > 3- > 4, you should return the list as 2- > 1- > 4- > 3.

**GIT URL:**

__Leet Code solution: Swap Nodes in Pairs solution__

__Java Solution__

__4). Number of Islands__Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

**:**

__Example 1__**Input**:

11110

11010

11000

00000

**Output**: 1

**:**

__Example 2__**Input**:

11000

11000

00100

00011

**Output**: 3

**Algorithm**:

Iterate the 2D array, every-time you encounter the 1:

a). increase the island count

b). recursively replace all the horizontally/vertically land(1) with water(0).

**GIT URL:**

__Leet Code solution: Number of Islands solution__

__Java Solution__**-K Himaanshu Shuklaa..**

## No comments:

## Post a Comment