diff --git a/Leetcode/RemoveNthNodeFromLinkedList.java b/Leetcode/RemoveNthNodeFromLinkedList.java new file mode 100644 index 0000000..f3fa972 --- /dev/null +++ b/Leetcode/RemoveNthNodeFromLinkedList.java @@ -0,0 +1,62 @@ +package Leetcode; +/** +Runtime: 1 ms, faster than 72.47% of Java online submissions for Remove Nth Node From End of List. +Memory Usage: 42.7 MB, less than 16.51% of Java online submissions for Remove Nth Node From End of List. + +Given the head of a linked list, remove the nth node from the end of the list and return its head. + + + +Example 1: + + +Input: head = [1,2,3,4,5], n = 2 +Output: [1,2,3,5] +Example 2: + +Input: head = [1], n = 1 +Output: [] +Example 3: + +Input: head = [1,2], n = 1 +Output: [1] + +**/ +public class RemoveNthNodeFromLinkedList { + + public static void main(String[] args) { + + } + + public ListNode removeNthFromEnd(ListNode head, int n) { + int i=0; + ListNode l = head; + //this loop is used to get the total length of linkedList + while(l!=null){ + l = l.next; + i++; + } + // The order of element to be removed from 1st element + int k=i-n; + //The first element is to be removed and there is only one element + if(k==0 && i==0) { return new ListNode();} + //if 1st element is to be removed and there are many elements + else if(k==0){ + return head.next; + } + i=0; + ListNode m = head; + l=head; + //traverse from 1st till i-n elements + while(i!=k){ + m=l; + l=l.next; + i++; + } + + m.next = l.next; + l.next = null; + return head; + + } +} diff --git a/Leetcode/TappingRainWater.java b/Leetcode/TappingRainWater.java new file mode 100644 index 0000000..e778a4f --- /dev/null +++ b/Leetcode/TappingRainWater.java @@ -0,0 +1,53 @@ +package Leetcode; +/* + * Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. + + +Example 1: + + +Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] +Output: 6 +Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. +Example 2: + +Input: height = [4,2,0,3,2,5] +Output: 9 + +Runtime: 1 ms, faster than 99.76% of Java online submissions for Trapping Rain Water. +Memory Usage: 48.3 MB, less than 76.32% of Java online submissions for Trapping Rain Water. + * + */ +public class TappingRainWater { + + public static void main(String[] args) { + var trw = new TappingRainWater(); + System.out.println(trw.trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1})); + } + + public int trap(int[] height) { + int ans = 0; int left = 0; int right = height.length-1; + int leftmax =0; + int rightmax =0; + while(left= leftmax){ + leftmax=height[left]; + } + else{ + ans+=(leftmax-height[left]); + } + left++; + } + else{ + + if(height[right]>=rightmax){ + rightmax=height[right]; + } + else{ans+=(rightmax-height[right]);} + right--; + } + } + return ans; + } +} diff --git a/SinglyLinkedList/CircularSinglyLinkedList.java b/SinglyLinkedList/CircularSinglyLinkedList.java index 62f96af..1b4bf1d 100644 --- a/SinglyLinkedList/CircularSinglyLinkedList.java +++ b/SinglyLinkedList/CircularSinglyLinkedList.java @@ -70,4 +70,24 @@ public void traverseLinkedList(){ System.out.println("All the elemenets are printed"); System.out.println(); } + + public void findNodeInLinkedList(int nodevalue){ + if(head==null){ + System.out.println("There is no linked list present"); + return; + } + Node test = head; + System.out.println("The elements are :"); + System.out.println(); + while(test.next!=head){ + System.out.print(test.value); + test=test.next; + System.out.print("-->"); + + } + System.out.println(test.value); + System.out.println(); + System.out.println("All the elemenets are printed"); + System.out.println(); + } }