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

Example:

`Input:  1->2->6->3->4->5->6, val = 6Output: 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.

--

--

Is a binary tree complete?

`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;            }`

--

--

Binary Tree Right Side View

`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;            }`

--

--

Build BST from sorted Array

`/** * 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;    }}`

--

--

Minimum Depth of Binary Tree

`/** * 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;    }    }`

--

--

Dynamic Programming — Strings

--

--

Smrita Pokharel

Exploring and discovering. Learning everyday.