Skip to content

Commit 68af84a

Browse files
authored
2025-01-30 v. 8.3.5: added "1171. Remove Zero Sum Consecutive Nodes from Linked List"
2 parents cc4b663 + 5382d95 commit 68af84a

4 files changed

+95
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
666666
| 1111. Maximum Nesting Depth of Two Valid Parentheses Strings | [Link](https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/) | [Link](./lib/medium/1111_maximum_nesting_depth_of_two_valid_parentheses_strings.rb) | [Link](./test/medium/test_1111_maximum_nesting_depth_of_two_valid_parentheses_strings.rb) |
667667
| 1123. Lowest Common Ancestor of Deepest Leaves | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/) | [Link](./lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb) | [Link](./test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb) |
668668
| 1161. Maximum Level Sum of a Binary Tree | [Link](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | [Link](./lib/medium/1161_maximum_level_sum_of_a_binary_tree.rb) | [Link](./test/medium/test_1161_maximum_level_sum_of_a_binary_tree.rb) |
669+
| 1171. Remove Zero Sum Consecutive Nodes from Linked List | [Link](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [Link](./lib/medium/1171_remove_zero_sum_consecutive_nodes_from_linked_list.rb) | [Link](./test/medium/test_1171_remove_zero_sum_consecutive_nodes_from_linked_list.rb) |
669670
| 1209. Remove All Adjacent Duplicates in String II | [Link](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [Link](./lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb) | [Link](./test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.rb) |
670671
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
671672
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.3.4'
8+
s.version = '8.3.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/
6+
# @param {ListNode} head
7+
# @return {ListNode}
8+
def remove_zero_sum_sublists(head)
9+
return unless head
10+
11+
dummy = ::ListNode.new(0)
12+
dummy.next = head
13+
prefix_sum = 0
14+
prefix_sums = {}
15+
current = dummy
16+
17+
while current
18+
prefix_sum += current.val
19+
20+
if prefix_sums[prefix_sum]
21+
prev = prefix_sums[prefix_sum]
22+
temp_sum = prefix_sum
23+
temp = prev.next
24+
25+
while temp != current
26+
temp_sum += temp.val
27+
prefix_sums.delete(temp_sum)
28+
temp = temp.next
29+
end
30+
31+
prev.next = current.next
32+
else
33+
prefix_sums[prefix_sum] = current
34+
end
35+
36+
current = current.next
37+
end
38+
39+
dummy.next
40+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/1171_remove_zero_sum_consecutive_nodes_from_linked_list'
6+
require 'minitest/autorun'
7+
8+
class RemoveZeroSumConsecutiveNodesFromLinkedListTest < ::Minitest::Test
9+
def test_default_one
10+
assert(
11+
::ListNode.are_equals(
12+
::ListNode.from_array(
13+
[3, 1]
14+
),
15+
remove_zero_sum_sublists(
16+
::ListNode.from_array(
17+
[1, 2, -3, 3, 1]
18+
)
19+
)
20+
)
21+
)
22+
end
23+
24+
def test_default_two
25+
assert(
26+
::ListNode.are_equals(
27+
::ListNode.from_array(
28+
[1, 2, 4]
29+
),
30+
remove_zero_sum_sublists(
31+
::ListNode.from_array(
32+
[1, 2, 3, -3, 4]
33+
)
34+
)
35+
)
36+
)
37+
end
38+
39+
def test_default_three
40+
assert(
41+
::ListNode.are_equals(
42+
::ListNode.from_array(
43+
[1]
44+
),
45+
remove_zero_sum_sublists(
46+
::ListNode.from_array(
47+
[1, 2, 3, -3, -2]
48+
)
49+
)
50+
)
51+
)
52+
end
53+
end

0 commit comments

Comments
 (0)