Skip to content

Commit e1c5489

Browse files
Merge pull request #19 from abhishektripathi66/singlelinkedlist
leetcode and circular single linked list
2 parents c006394 + 6c37357 commit e1c5489

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Leetcode;
2+
/**
3+
Runtime: 1 ms, faster than 72.47% of Java online submissions for Remove Nth Node From End of List.
4+
Memory Usage: 42.7 MB, less than 16.51% of Java online submissions for Remove Nth Node From End of List.
5+
6+
Given the head of a linked list, remove the nth node from the end of the list and return its head.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: head = [1,2,3,4,5], n = 2
14+
Output: [1,2,3,5]
15+
Example 2:
16+
17+
Input: head = [1], n = 1
18+
Output: []
19+
Example 3:
20+
21+
Input: head = [1,2], n = 1
22+
Output: [1]
23+
24+
**/
25+
public class RemoveNthNodeFromLinkedList {
26+
27+
public static void main(String[] args) {
28+
29+
}
30+
31+
public ListNode removeNthFromEnd(ListNode head, int n) {
32+
int i=0;
33+
ListNode l = head;
34+
//this loop is used to get the total length of linkedList
35+
while(l!=null){
36+
l = l.next;
37+
i++;
38+
}
39+
// The order of element to be removed from 1st element
40+
int k=i-n;
41+
//The first element is to be removed and there is only one element
42+
if(k==0 && i==0) { return new ListNode();}
43+
//if 1st element is to be removed and there are many elements
44+
else if(k==0){
45+
return head.next;
46+
}
47+
i=0;
48+
ListNode m = head;
49+
l=head;
50+
//traverse from 1st till i-n elements
51+
while(i!=k){
52+
m=l;
53+
l=l.next;
54+
i++;
55+
}
56+
57+
m.next = l.next;
58+
l.next = null;
59+
return head;
60+
61+
}
62+
}

Leetcode/TappingRainWater.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package Leetcode;
2+
/*
3+
* 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.
4+
5+
6+
Example 1:
7+
8+
9+
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
10+
Output: 6
11+
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.
12+
Example 2:
13+
14+
Input: height = [4,2,0,3,2,5]
15+
Output: 9
16+
17+
Runtime: 1 ms, faster than 99.76% of Java online submissions for Trapping Rain Water.
18+
Memory Usage: 48.3 MB, less than 76.32% of Java online submissions for Trapping Rain Water.
19+
*
20+
*/
21+
public class TappingRainWater {
22+
23+
public static void main(String[] args) {
24+
var trw = new TappingRainWater();
25+
System.out.println(trw.trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1}));
26+
}
27+
28+
public int trap(int[] height) {
29+
int ans = 0; int left = 0; int right = height.length-1;
30+
int leftmax =0;
31+
int rightmax =0;
32+
while(left<right){
33+
if(height[left]<height[right]){
34+
if(height[left]>= leftmax){
35+
leftmax=height[left];
36+
}
37+
else{
38+
ans+=(leftmax-height[left]);
39+
}
40+
left++;
41+
}
42+
else{
43+
44+
if(height[right]>=rightmax){
45+
rightmax=height[right];
46+
}
47+
else{ans+=(rightmax-height[right]);}
48+
right--;
49+
}
50+
}
51+
return ans;
52+
}
53+
}

SinglyLinkedList/CircularSinglyLinkedList.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,24 @@ public void traverseLinkedList(){
7070
System.out.println("All the elemenets are printed");
7171
System.out.println();
7272
}
73+
74+
public void findNodeInLinkedList(int nodevalue){
75+
if(head==null){
76+
System.out.println("There is no linked list present");
77+
return;
78+
}
79+
Node test = head;
80+
System.out.println("The elements are :");
81+
System.out.println();
82+
while(test.next!=head){
83+
System.out.print(test.value);
84+
test=test.next;
85+
System.out.print("-->");
86+
87+
}
88+
System.out.println(test.value);
89+
System.out.println();
90+
System.out.println("All the elemenets are printed");
91+
System.out.println();
92+
}
7393
}

0 commit comments

Comments
 (0)