Skip to content

Commit 8700284

Browse files
authored
Merge pull request #171 from fartem/remove-nodes-from-linked-list
2023-01-22 update: added "2487. Remove Nodes From Linked List"
2 parents 954896f + f7c79ed commit 8700284

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
744744
| 2375. Construct Smallest Number From DI String | [Link](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/ConstructSmallestNumberFromDIString.java) |
745745
| 2390. Removing Stars From a String | [Link](https://leetcode.com/problems/removing-stars-from-a-string/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/RemovingStarsFromAString.java) |
746746
| 2415. Reverse Odd Levels of Binary Tree | [Link](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/ReverseOddLevelsOfBinaryTree.java) |
747+
| 2487. Remove Nodes From Linked List | [Link](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/medium/RemoveNodesFromLinkedList.java) |
747748

748749
### Hard
749750

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import com.smlnskgmail.jaman.leetcodejava.support.ListNode;
4+
5+
// https://leetcode.com/problems/remove-nodes-from-linked-list/
6+
public class RemoveNodesFromLinkedList {
7+
8+
private final ListNode input;
9+
10+
public RemoveNodesFromLinkedList(ListNode input) {
11+
this.input = input;
12+
}
13+
14+
public ListNode solution() {
15+
return removeNodes(input);
16+
}
17+
18+
private ListNode removeNodes(ListNode head) {
19+
if (head == null) {
20+
return null;
21+
}
22+
head.next = removeNodes(head.next);
23+
return head.next != null && head.val < head.next.val ? head.next : head;
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import com.smlnskgmail.jaman.leetcodejava.support.ListNode;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class RemoveNodesFromLinkedListTest {
9+
10+
@Test
11+
public void defaultTests() {
12+
assertTrue(
13+
ListNode.areEqual(
14+
ListNode.fromNumbers(13, 8),
15+
new RemoveNodesFromLinkedList(
16+
ListNode.fromNumbers(5, 2, 13, 3, 8)
17+
).solution()
18+
)
19+
);
20+
assertTrue(
21+
ListNode.areEqual(
22+
ListNode.fromNumbers(1, 1, 1, 1),
23+
new RemoveNodesFromLinkedList(
24+
ListNode.fromNumbers(1, 1, 1, 1)
25+
).solution()
26+
)
27+
);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)