Skip to content

Commit d6e414b

Browse files
committed
Added LinkedList problems
1 parent cc0538d commit d6e414b

3 files changed

Lines changed: 205 additions & 4 deletions

File tree

README.md

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Computer Science problems in Java.
22

3+
### Topics covered
4+
1. LinkedList
5+
1. Fast and Slow Pointer
6+
1. Midpoint of LinkedList
7+
2. Nth element removal from the end of the list
8+
39
## Backlog
410

511
### Problems
@@ -9,10 +15,6 @@ Computer Science problems in Java.
915
2. You have N cars numbered from 1 to N. Car 1 is open and all the other cars are closed. Each car has a list of keys
1016
where key i can open the car i. Find out if we can open all the cars.
1117

12-
### Design
13-
14-
1. LLD of chess
15-
1618
### Patterns
1719

1820
1. Two Pointers
@@ -475,3 +477,90 @@ Computer Science problems in Java.
475477
* Batch framework for Java — https://github.com/j-easy/easy-batch
476478
* Object-functional extension for Java — https://github.com/vavr-io/vavr
477479
* Java binary serialization and cloning — https://github.com/EsotericSoftware/kryo
480+
481+
### Design
482+
483+
1. LLD of chess
484+
485+
486+
### System Design Problems
487+
488+
#### Basics
489+
490+
1. Design a chess game
491+
2. Design a simple URL shortening service.
492+
3. Design a basic chat application.
493+
4. Design a file storage system.
494+
5. Design a simple social media platform.
495+
6. Design a simple search engine.
496+
7. Design a simple e-commerce website.
497+
8. Design a basic ride-sharing system.
498+
9. Design a basic video streaming service.
499+
10. Design a simple recommendation system.
500+
11. Design a basic food delivery app.
501+
12. Design a parking lot management system.
502+
13. Design a simple music streaming service.
503+
14. Design a basic online ticket booking system.
504+
15. Design a simple note-taking application.
505+
16. Design a weather forecasting system.
506+
17. Design a basic email service.
507+
18. Design a file synchronization system.
508+
19. Design a simple calendar application.
509+
20. Design a basic online quiz platform.
510+
21. Design a user authentication system.
511+
512+
#### Advanced
513+
514+
1. Design a URL-shortening service like bit.ly.
515+
2. Design a distributed key-value store like Redis.
516+
3. Design a scalable social network like Facebook.
517+
4. Design a scalable recommendation system like Netflix.
518+
5. Design a distributed file system like Hadoop's HDFS.
519+
6. Design a real-time messaging system like WhatsApp.
520+
7. Design a web crawler like Google.
521+
8. Design a distributed cache like Memcached.
522+
9. Design a content delivery network (CDN) like Cloudflare.
523+
10. Design a scalable search engine like Google.
524+
11. Design a ride-sharing system like Uber.
525+
12. Design a video streaming service like YouTube.
526+
13. Design an online food delivery system like Zomato.
527+
14. Design a collaborative document editing system like Google Docs.
528+
15. Design an e-commerce platform like Amazon.
529+
16. Design a recommendation system for an online marketplace.
530+
17. Design a fault-tolerant distributed database system.
531+
18. Design a scalable event-driven system like Twitter.
532+
19. Design a scalable photo-sharing platform like Instagram.
533+
20. Design a distributed task scheduling system.
534+
535+
### Design Patterns
536+
537+
#### Creational patterns
538+
These patterns provide various object creation mechanisms, which increase the flexibility and reuse of existing code.
539+
540+
1. Factory Method
541+
2. Abstract Factory
542+
3. Builder
543+
4. Prototype
544+
5. Singleton
545+
546+
547+
#### Structural patterns
548+
These patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
549+
550+
1. Adapter
551+
2. Bridge
552+
3. Composite
553+
4. Decorator
554+
5. Facade
555+
6. Flyweight
556+
7. Proxy
557+
558+
#### Behavioral patterns
559+
These patterns are concerned with algorithms and the assignment of responsibilities between objects.
560+
561+
1. Chain of Responsibility
562+
2. Command
563+
3. Iterator
564+
4. Mediator
565+
5. Memento
566+
6. Observer

src/main/java/org/dojo/leetcode/LinkedListProblems.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
import static org.dojo.leetcode.SinglyLinkedList.SinglyLinkedListNode;
44

55
public class LinkedListProblems {
6+
public static class ListNode {
7+
int val;
8+
ListNode next;
9+
ListNode() { }
10+
ListNode(int val) { this.val = val; }
11+
ListNode(int val, ListNode next) {
12+
this.val = val;
13+
this.next = next;
14+
}
15+
}
16+
617
public SinglyLinkedListNode addTwoNumbers(SinglyLinkedListNode l1, SinglyLinkedListNode l2) {
718
var carryover = 0;
819
var sumNode = new SinglyLinkedListNode();
@@ -92,4 +103,32 @@ public SinglyLinkedListNode mergeTwoLists(SinglyLinkedListNode list1, SinglyLink
92103

93104
return result;
94105
}
106+
107+
public ListNode removeNthFromEnd(ListNode head, int n) {
108+
ListNode slow = head, fast = head;
109+
110+
int count = 1;
111+
for (int i = 0; i < n; i++) {
112+
if (fast.next == null) break;
113+
114+
fast = fast.next;
115+
count++;
116+
}
117+
if (count == n) return head.next;
118+
119+
while (fast.next != null) {
120+
slow = slow.next;
121+
fast = fast.next;
122+
}
123+
124+
125+
slow.next = slow.next.next;
126+
127+
return head;
128+
}
129+
130+
public void deleteNode(ListNode node) {
131+
node.val = node.next.val;
132+
node.next = node.next.next;
133+
}
95134
}

src/test/java/org/dojo/leetcode/LinkedListProblemsTests.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.dojo.leetcode;
22

3+
import org.dojo.leetcode.LinkedListProblems.ListNode;
34
import org.dojo.leetcode.SinglyLinkedList.SinglyLinkedListNode;
45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.Arguments;
@@ -8,6 +9,8 @@
89
import java.util.stream.Stream;
910

1011
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
import static org.junit.jupiter.api.Assertions.assertNull;
1114

1215
class LinkedListProblemsTests {
1316
private final LinkedListProblems sut = new LinkedListProblems();
@@ -74,4 +77,74 @@ void mergeTwoLists(SinglyLinkedListNode list1, SinglyLinkedListNode list2, Singl
7477
actual = actual.getNext();
7578
}
7679
}
80+
81+
private static ListNode linkedListNodeFromArray(int[] arr) {
82+
if (arr == null || arr.length == 0) return null;
83+
84+
ListNode head = new ListNode(arr[0]);
85+
ListNode current = head;
86+
87+
for (int i = 1; i < arr.length; i++) {
88+
ListNode newNode = new ListNode(arr[i]);
89+
current.next = newNode;
90+
current = newNode;
91+
}
92+
93+
return head;
94+
}
95+
96+
private static Stream<Arguments> removeNthFromEndTestData() {
97+
return Stream.of(
98+
Arguments.of(
99+
linkedListNodeFromArray(new int[] {1, 2, 3, 4, 5}),
100+
2,
101+
linkedListNodeFromArray(new int[] {1, 2, 3, 5})
102+
),
103+
104+
Arguments.of(
105+
linkedListNodeFromArray(new int[] {1}),
106+
1,
107+
null
108+
),
109+
110+
Arguments.of(
111+
linkedListNodeFromArray(new int[] {1, 2}),
112+
1,
113+
linkedListNodeFromArray(new int[] {1})
114+
),
115+
116+
Arguments.of(
117+
linkedListNodeFromArray(new int[] {1, 2, 3}),
118+
3,
119+
linkedListNodeFromArray(new int[] {2, 3})
120+
),
121+
122+
Arguments.of(
123+
linkedListNodeFromArray(new int[] {1, 2, 3, 4, 5, 6}),
124+
3,
125+
linkedListNodeFromArray(new int[] {1, 2, 3, 5, 6})
126+
)
127+
);
128+
}
129+
130+
@ParameterizedTest
131+
@MethodSource("removeNthFromEndTestData")
132+
void removeNthFromEnd(ListNode head, int n, ListNode expectedList) {
133+
ListNode actual = sut.removeNthFromEnd(head, n);
134+
135+
if (expectedList == null) {
136+
assertNull(actual);
137+
return;
138+
}
139+
140+
ListNode expected = expectedList;
141+
while (expected != null) {
142+
assertNotNull(actual, "Actual list is shorter than expected");
143+
assertEquals(expected.val, actual.val);
144+
expected = expected.next;
145+
actual = actual.next;
146+
}
147+
148+
assertNull(actual, "Actual list is longer than expected");
149+
}
77150
}

0 commit comments

Comments
 (0)