Skip to content

Commit 46752ff

Browse files
add 1171
1 parent e4b6553 commit 46752ff

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ _If you like this project, please leave me a star._ ★
257257
|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java) | |Easy|Math, String|
258258
|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java) | |Easy|Array, Sliding Window|
259259
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java) | |Easy|Math|
260+
|1171|[Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1171.java) | |Medium|LinkedList|
260261
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | |Easy||
261262
|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1161.java) | |Medium|Graph|
262263
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| |Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
import java.util.*;
6+
7+
public class _1171 {
8+
public static class Solution1 {
9+
/**
10+
* I keep shrinking the array whenever I found there's a range of sum that equals to zero
11+
* until the size of the list doesn't change any more.
12+
* This is probably not super efficient, but accepted on LeetCode.
13+
*/
14+
public ListNode removeZeroSumSublists(ListNode head) {
15+
List<Integer> list = convertToList(head);
16+
int size;
17+
do {
18+
size = list.size();
19+
list = shrinkList(list);
20+
} while (list.size() != size);
21+
return recoverLinkedList(list);
22+
}
23+
24+
private ListNode recoverLinkedList(List<Integer> list) {
25+
ListNode pre = new ListNode(-1);
26+
ListNode tmp = pre;
27+
for (int i = 0; i < list.size(); i++) {
28+
tmp.next = new ListNode(list.get(i));
29+
tmp = tmp.next;
30+
}
31+
return pre.next;
32+
}
33+
34+
private List<Integer> convertToList(ListNode head) {
35+
List<Integer> list = new ArrayList<>();
36+
while (head != null) {
37+
if (head.val != 0) {
38+
//if it's zero, we'll just ignore it, this can help us take care of the zero values
39+
list.add(head.val);
40+
}
41+
head = head.next;
42+
}
43+
return list;
44+
}
45+
46+
private List<Integer> shrinkList(List<Integer> list) {
47+
for (int i = 0; i < list.size(); i++) {
48+
int start = i;
49+
List<Integer> preSumList = new ArrayList<>();
50+
for (int k = 0; k < start; k++) {
51+
preSumList.add(0);
52+
}
53+
preSumList.add(list.get(i));
54+
for (int k = i; k < list.size(); k++) {
55+
if (k > start) {
56+
Integer sum = preSumList.get(k - 1) + list.get(k);
57+
if (sum == 0) {
58+
List<Integer> shrinkedList = new ArrayList<>();
59+
for (int j = 0; j < start; j++) {
60+
shrinkedList.add(list.get(j));
61+
}
62+
for (int j = k + 1; j < list.size(); j++) {
63+
shrinkedList.add(list.get(j));
64+
}
65+
return shrinkedList;
66+
} else {
67+
preSumList.add(sum);
68+
}
69+
}
70+
}
71+
}
72+
return list;
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.LinkedListUtils;
4+
import com.fishercoder.solutions._1171;
5+
import com.fishercoder.solutions._96;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1171Test {
12+
private static _1171.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1171.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{3, 1}), solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[]{1, 2, -3, 3, 1})));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{1, 2, 4}), solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, -3, 4})));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{1}), solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, -3, -2})));
32+
}
33+
34+
@Test
35+
public void test4() {
36+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{5, -2, -5}),
37+
solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[]{5, -3, -4, 1, 6, -2, -5})));
38+
}
39+
40+
@Test
41+
public void test5() {
42+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{}),
43+
solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[]{0})));
44+
}
45+
46+
@Test
47+
public void test6() {
48+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{2}),
49+
solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(new int[]{2, 0})));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)