Remove all elements from a linked list of integers that have value val.

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

SOLUTION

class Solution {
public ListNode removeElements(ListNode head, int val) {

ListNode current = head;
ListNode prev = null;

while(current!=null){
if(current.val ==val){
if(prev!=null){
prev.next = current.next;
current.next = null;
current = prev.next;
}else{
head = head.next;
current = current.next;
}
}
else{
prev = current;
current = current.next;
}
}
return head;
}
}

Runtime: 1 ms, faster than 87.14% of Java online submissions for Remove Linked List Elements.

Memory Usage: 40.1 MB, less than 87.67% of Java online submissions for Remove Linked List Elements.

public boolean isCompleteTree(TreeNode root) {

Queue<TreeNode> queue = new LinkedList<TreeNode>();

queue.add(root);

boolean isAbsent = false;

while(!queue.isEmpty()){

TreeNode node = queue.remove();

if(node==null){
isAbsent = true;
}else{
if(isAbsent){
return false;
}
queue.add(node.left);
queue.add(node.right);
}

}

return true;

}
public List<Integer> rightSideView(TreeNode root) {

return rightSideViewBFS(root);
}

private List<Integer> rightSideViewBFS(TreeNode root){

List<Integer> result = new ArrayList<Integer>();

if(root==null){
return result;
}

Queue<TreeNode> queue = new LinkedList<TreeNode>();

queue.add(root);

while(queue.size() != 0){
int size = queue.size();

for (int i=0; i<size; i++) {

TreeNode node = queue.remove();

if(i==0){
result.add(node.val);
}

if(node.right!=null){
queue.add(node.right);
}

if(node.left!=null){
queue.add(node.left);
}

}
}

return result;

}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {

return buildTree(0,nums.length-1,nums);
}

public TreeNode buildTree(int start, int end, int[] nums){

if(start>end){
return null;
}

int mid = (start+end)/2;

TreeNode root = new TreeNode(nums[mid]);

root.left = buildTree(start,mid-1,nums);
root.right = buildTree(mid+1,end,nums);

return root;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

public int minDepth(TreeNode root) {

if(root==null){
return 0;
}

Queue<TreeNode> queue = new LinkedList<TreeNode>();

queue.add(root);

int level = 1;
int minDepth = 1000000000;

while(!queue.isEmpty()){

int size = queue.size();

for(int i=0;i<size;i++){
TreeNode node = queue.remove();

if(node!=null && node.left==null && node.right==null){
if(minDepth>level){
return level;
}

}else{
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
}

}
level ++;
}

return minDepth;
}

}

Approach 1 : Recursion without memoization

public int longestCommonSubsequence(String text1, String text2) {
return longestCommonSubsequenceRecursion(0,0,text1,text2);
}

private int longestCommonSubsequenceRecursion(int index1,int index2, String text1, String text2){…

GRAPH REPRESENTATION

There are 3 ways in which we can represent graphs:

  1. Array of edges
  2. Adjacency Matrix
  3. Adjacency List.

DEPTH FIRST SEARCH

  1. Recursive <
  2. Iterative -> Stack

BREADTH FIRST SEARCH

  1. Recursive is hard.
  2. Iterative

CODE

public class GraphRep {

class Node{…

Smrita Pokharel

Exploring and discovering. Learning everyday.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store