Skip to content

Commit acaf76c

Browse files
committed
Add Linked List, Stack and Queue solutions
1 parent 2884730 commit acaf76c

10 files changed

+462
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ leetcode-75-solutions-java/
8888
| Hash Map / Set | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [UniqueOccurrenceChecker.java](src/hash_map_set/UniqueOccurrenceChecker.java) | Easy |
8989
| Hash Map / Set | [Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/) | [StringCloseChecker.java](src/hash_map_set/StringCloseChecker.java) | Medium |
9090
| Hash Map / Set | [Equal Row and Column Pairs](https://leetcode.com/problems/equal-row-and-column-pairs/) | [MatrixPairs.java](src/hash_map_set/MatrixPairs.java) | Medium |
91+
| Stack | [Decode String](https://leetcode.com/problems/decode-string/) | [StringDecoder.java](src/stack/StringDecoder.java) | Medium |
92+
| Stack | [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [AsteroidCollisionSimulator.java](src/stack/AsteroidCollisionSimulator.java) | Medium |
93+
| Stack | [Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/) | [StarStringProcessor.java](src/stack/StarStringProcessor.java) | Medium |
94+
| Queue | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [RecentCounter.java](src/queue/RecentCounter.java) | Easy |
95+
| Queue | [Dota2 Senate](https://leetcode.com/problems/dota2-senate/) | [SenateDecisionSimulator.java](src/queue/SenateDecisionSimulator.java) | Medium |
96+
| Linked List | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [LinkedListProcessor.java](src/linked_list/LinkedListProcessor.java) | Medium |
97+
| Linked List | [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) | [OddEvenLinkedListTransformer.java](src/linked_list/OddEvenLinkedListTransformer.java) | Medium |
98+
| Linked List | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [LinkedListReverser.java](src/linked_list/LinkedListReverser.java) | Easy |
99+
| Linked List | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [MaxTwinSumCalculator.java](src/linked_list/MaxTwinSumCalculator.java) | Medium |
91100
| ... | ... | ... | ... |
92101
| Trie | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) | [PrefixTrie.java](src/trie/PrefixTrie.java) | Medium |
93102
| Trie | [Search Suggestions System](https://leetcode.com/problems/search-suggestions-system) | [SearchSuggestionsSystem.java](src/trie/SearchSuggestionsSystem.java) | Medium |
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package linked_list;
2+
3+
public class LinkedListProcessor {
4+
5+
static class ListNode {
6+
int val;
7+
ListNode next;
8+
9+
ListNode(int val) {
10+
this.val = val;
11+
}
12+
}
13+
14+
public ListNode deleteMiddle(ListNode head) {
15+
if (head == null || head.next == null) return null;
16+
17+
ListNode fast = head, slow = head, prev = null;
18+
19+
while (fast != null && fast.next != null) {
20+
fast = fast.next.next;
21+
prev = slow;
22+
slow = slow.next;
23+
}
24+
25+
prev.next = slow.next;
26+
27+
return head;
28+
}
29+
30+
private static ListNode arrayToList(int[] arr) {
31+
ListNode dummy = new ListNode(0);
32+
ListNode tail = dummy;
33+
for (int num : arr) {
34+
tail.next = new ListNode(num);
35+
tail = tail.next;
36+
}
37+
return dummy.next;
38+
}
39+
40+
private static boolean areListsEqual(ListNode l1, ListNode l2) {
41+
while (l1 != null && l2 != null) {
42+
if (l1.val != l2.val) return false;
43+
l1 = l1.next;
44+
l2 = l2.next;
45+
}
46+
return l1 == null && l2 == null;
47+
}
48+
49+
public static void main(String[] args) {
50+
LinkedListProcessor processor = new LinkedListProcessor();
51+
52+
ListNode head1 = arrayToList(new int[]{1,3,4,7,1,2,6});
53+
ListNode expected1 = arrayToList(new int[]{1,3,4,1,2,6});
54+
assert areListsEqual(processor.deleteMiddle(head1), expected1) : "Test case 1 failed";
55+
56+
ListNode head2 = arrayToList(new int[]{1,2,3,4});
57+
ListNode expected2 = arrayToList(new int[]{1,2,4});
58+
assert areListsEqual(processor.deleteMiddle(head2), expected2) : "Test case 2 failed";
59+
60+
ListNode head3 = arrayToList(new int[]{2,1});
61+
ListNode expected3 = arrayToList(new int[]{2});
62+
assert areListsEqual(processor.deleteMiddle(head3), expected3) : "Test case 3 failed";
63+
64+
System.out.println("All test cases passed!");
65+
}
66+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package linked_list;
2+
3+
4+
public class LinkedListReverser {
5+
6+
static class ListNode {
7+
int val;
8+
ListNode next;
9+
10+
ListNode(int val) {
11+
this.val = val;
12+
}
13+
}
14+
15+
public ListNode reverseList(ListNode head) {
16+
ListNode prev = null;
17+
ListNode current = head;
18+
19+
while (current != null) {
20+
ListNode next = current.next;
21+
current.next = prev;
22+
prev = current;
23+
current = next;
24+
}
25+
return prev;
26+
}
27+
28+
private static ListNode arrayToList(int[] arr) {
29+
ListNode dummy = new ListNode(0);
30+
ListNode tail = dummy;
31+
for (int num : arr) {
32+
tail.next = new ListNode(num);
33+
tail = tail.next;
34+
}
35+
return dummy.next;
36+
}
37+
38+
private static boolean areListsEqual(ListNode l1, ListNode l2) {
39+
while (l1 != null && l2 != null) {
40+
if (l1.val != l2.val) return false;
41+
l1 = l1.next;
42+
l2 = l2.next;
43+
}
44+
return l1 == null && l2 == null;
45+
}
46+
47+
public static void main(String[] args) {
48+
LinkedListReverser reverser = new LinkedListReverser();
49+
50+
ListNode head1 = arrayToList(new int[]{1,2,3,4,5});
51+
ListNode expected1 = arrayToList(new int[]{5,4,3,2,1});
52+
assert areListsEqual(reverser.reverseList(head1), expected1) : "Test case 1 failed";
53+
54+
ListNode head2 = arrayToList(new int[]{1,2});
55+
ListNode expected2 = arrayToList(new int[]{2,1});
56+
assert areListsEqual(reverser.reverseList(head2), expected2) : "Test case 2 failed";
57+
58+
ListNode head3 = arrayToList(new int[]{});
59+
ListNode expected3 = arrayToList(new int[]{});
60+
assert areListsEqual(reverser.reverseList(head3), expected3) : "Test case 3 failed";
61+
62+
System.out.println("All test cases passed!");
63+
}
64+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package linked_list;
2+
3+
public class MaxTwinSumCalculator {
4+
5+
static class ListNode {
6+
int val;
7+
ListNode next;
8+
9+
ListNode(int val) {
10+
this.val = val;
11+
}
12+
}
13+
14+
public int pairSum(ListNode head) {
15+
ListNode slow = head, fast = head;
16+
int maxSum = 0;
17+
18+
// Finding the middle of the list
19+
while (fast != null && fast.next != null) {
20+
slow = slow.next;
21+
fast = fast.next.next;
22+
}
23+
24+
// Reversing the second half of the list
25+
ListNode nextNode, prev = null;
26+
while (slow != null) {
27+
nextNode = slow.next;
28+
slow.next = prev;
29+
prev = slow;
30+
slow = nextNode;
31+
}
32+
33+
// Calculating max twin sum
34+
while (prev != null) {
35+
maxSum = Math.max(maxSum, head.val + prev.val);
36+
head = head.next;
37+
prev = prev.next;
38+
}
39+
40+
return maxSum;
41+
}
42+
43+
private static ListNode arrayToList(int[] arr) {
44+
ListNode dummy = new ListNode(0);
45+
ListNode tail = dummy;
46+
for (int num : arr) {
47+
tail.next = new ListNode(num);
48+
tail = tail.next;
49+
}
50+
return dummy.next;
51+
}
52+
53+
public static void main(String[] args) {
54+
MaxTwinSumCalculator calculator = new MaxTwinSumCalculator();
55+
56+
ListNode head1 = arrayToList(new int[]{5,4,2,1});
57+
assert calculator.pairSum(head1) == 6 : "Test case 1 failed";
58+
59+
ListNode head2 = arrayToList(new int[]{4,2,2,3});
60+
assert calculator.pairSum(head2) == 7 : "Test case 2 failed";
61+
62+
ListNode head3 = arrayToList(new int[]{1,100000});
63+
assert calculator.pairSum(head3) == 100001 : "Test case 3 failed";
64+
65+
System.out.println("All test cases passed!");
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package linked_list;
2+
3+
public class OddEvenLinkedListTransformer {
4+
5+
static class ListNode {
6+
int val;
7+
ListNode next;
8+
9+
ListNode(int val) {
10+
this.val = val;
11+
}
12+
}
13+
14+
public ListNode oddEvenList(ListNode head) {
15+
if (head == null || head.next == null) {
16+
return head;
17+
}
18+
19+
ListNode odd = head, even = head.next, evenHead = even;
20+
21+
while (even != null && even.next != null) {
22+
odd.next = odd.next.next;
23+
even.next = even.next.next;
24+
odd = odd.next;
25+
even = even.next;
26+
}
27+
28+
odd.next = evenHead;
29+
return head;
30+
}
31+
32+
private static ListNode arrayToList(int[] arr) {
33+
ListNode dummy = new ListNode(0);
34+
ListNode tail = dummy;
35+
for (int num : arr) {
36+
tail.next = new ListNode(num);
37+
tail = tail.next;
38+
}
39+
return dummy.next;
40+
}
41+
42+
private static boolean areListsEqual(ListNode l1, ListNode l2) {
43+
while (l1 != null && l2 != null) {
44+
if (l1.val != l2.val) return false;
45+
l1 = l1.next;
46+
l2 = l2.next;
47+
}
48+
return l1 == null && l2 == null;
49+
}
50+
51+
public static void main(String[] args) {
52+
OddEvenLinkedListTransformer transformer = new OddEvenLinkedListTransformer();
53+
54+
ListNode head1 = arrayToList(new int[]{1,2,3,4,5});
55+
ListNode expected1 = arrayToList(new int[]{1,3,5,2,4});
56+
assert areListsEqual(transformer.oddEvenList(head1), expected1) : "Test case 1 failed";
57+
58+
ListNode head2 = arrayToList(new int[]{2,1,3,5,6,4,7});
59+
ListNode expected2 = arrayToList(new int[]{2,3,6,7,1,5,4});
60+
assert areListsEqual(transformer.oddEvenList(head2), expected2) : "Test case 2 failed";
61+
62+
System.out.println("All test cases passed!");
63+
}
64+
}

src/queue/RecentCounter.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package queue;
2+
3+
import java.util.*;
4+
5+
class RecentCounter {
6+
7+
Queue<Integer> queue;
8+
9+
public RecentCounter() {
10+
queue = new LinkedList<>();
11+
}
12+
13+
public int ping(int t) {
14+
queue.offer(t);
15+
while (queue.peek() < t - 3000) {
16+
queue.poll();
17+
}
18+
return queue.size();
19+
}
20+
21+
22+
public static void main(String[] args) {
23+
RecentCounter counter = new RecentCounter();
24+
25+
// Test cases
26+
boolean passed = true;
27+
passed &= counter.ping(1) == 1;
28+
passed &= counter.ping(100) == 2;
29+
passed &= counter.ping(3001) == 3;
30+
passed &= counter.ping(3002) == 3;
31+
32+
if (passed) {
33+
System.out.println("All test cases passed!");
34+
} else {
35+
System.out.println("Some test cases failed.");
36+
}
37+
}
38+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package queue;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class SenateDecisionSimulator {
7+
8+
public String predictPartyVictory(String senate) {
9+
Queue<Integer> radiant = new LinkedList<>();
10+
Queue<Integer> dire = new LinkedList<>();
11+
int n = senate.length();
12+
13+
for (int i = 0; i < n; i++) {
14+
if (senate.charAt(i) == 'R') {
15+
radiant.offer(i);
16+
} else {
17+
dire.offer(i);
18+
}
19+
}
20+
21+
while (!radiant.isEmpty() && !dire.isEmpty()) {
22+
int rIndex = radiant.poll();
23+
int dIndex = dire.poll();
24+
if (rIndex < dIndex) {
25+
radiant.offer(rIndex + n);
26+
} else {
27+
dire.offer(dIndex + n);
28+
}
29+
}
30+
31+
return radiant.isEmpty() ? "Dire" : "Radiant";
32+
}
33+
34+
public static void main(String[] args) {
35+
SenateDecisionSimulator simulator = new SenateDecisionSimulator();
36+
37+
assert simulator.predictPartyVictory("RD").equals("Radiant") : "Test case 1 failed";
38+
assert simulator.predictPartyVictory("RDD").equals("Dire") : "Test case 2 failed";
39+
40+
System.out.println("All test cases passed!");
41+
}
42+
}

0 commit comments

Comments
 (0)