Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2bc2237

Browse files
add a second solution for 1171
1 parentd8c4ff5 commit2bc2237

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_1171.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,37 @@ private List<Integer> shrinkList(List<Integer> list) {
7272
returnlist;
7373
}
7474
}
75+
76+
publicstaticclassSolution2 {
77+
/**
78+
* credit: https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/discuss/366337/Java-Iterative-and-Recursive-solution
79+
* this post explains it all
80+
* key of the hashmap is the prefix sum of all the nodes we've gone so far
81+
* value of the hashmap is the corresponding linked list node
82+
*/
83+
publicListNoderemoveZeroSumSublists(ListNodehead) {
84+
ListNodepre =newListNode(-1);
85+
ListNodecurr =pre;
86+
pre.next =head;
87+
Map<Integer,ListNode>map =newHashMap<>();
88+
intpreSum =0;
89+
while (curr !=null) {
90+
preSum +=curr.val;
91+
if (map.containsKey(preSum)) {
92+
curr =map.get(preSum).next;
93+
intkey =preSum +curr.val;
94+
while (key !=preSum) {
95+
map.remove(key);
96+
curr =curr.next;
97+
key +=curr.val;
98+
}
99+
map.get(preSum).next =curr.next;
100+
}else {
101+
map.put(preSum,curr);
102+
}
103+
curr =curr.next;
104+
}
105+
returnpre.next;
106+
}
107+
}
75108
}

‎src/test/java/com/fishercoder/_1171Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010

1111
publicclass_1171Test {
1212
privatestatic_1171.Solution1solution1;
13+
privatestatic_1171.Solution2solution2;
1314

1415
@BeforeClass
1516
publicstaticvoidsetup() {
1617
solution1 =new_1171.Solution1();
18+
solution2 =new_1171.Solution2();
1719
}
1820

1921
@Test
2022
publicvoidtest1() {
2123
assertEquals(LinkedListUtils.contructLinkedList(newint[]{3,1}),solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2, -3,3,1})));
24+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{3,1}),solution2.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2, -3,3,1})));
2225
}
2326

2427
@Test
2528
publicvoidtest2() {
2629
assertEquals(LinkedListUtils.contructLinkedList(newint[]{1,2,4}),solution1.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2,3, -3,4})));
30+
assertEquals(LinkedListUtils.contructLinkedList(newint[]{1,2,4}),solution2.removeZeroSumSublists(LinkedListUtils.contructLinkedList(newint[]{1,2,3, -3,4})));
2731
}
2832

2933
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp