Skip to content

Commit

Permalink
Merge pull request #19 from abhishektripathi66/singlelinkedlist
Browse files Browse the repository at this point in the history
leetcode and circular single linked list
  • Loading branch information
abhishektripathi66 authored Sep 11, 2022
2 parents c006394 + 6c37357 commit e1c5489
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Leetcode/RemoveNthNodeFromLinkedList.java
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;

}
}
53 changes: 53 additions & 0 deletions Leetcode/TappingRainWater.java
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;
}
}
20 changes: 20 additions & 0 deletions SinglyLinkedList/CircularSinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit e1c5489

Please sign in to comment.