-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from abhishektripathi66/singlelinkedlist
leetcode and circular single linked list
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<right){ | ||
if(height[left]<height[right]){ | ||
if(height[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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters