Skip to content

Commit 725e522

Browse files
committed
2024-07-23 v. 6.2.9: added "82. Remove Duplicates from Sorted List II"
1 parent c4b2b3a commit 725e522

4 files changed

+60
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
494494
| 74. Search a 2D Matrix | [Link](https://leetcode.com/problems/search-a-2d-matrix/) | [Link](./lib/medium/74_search_a_2d_matrix.rb) |
495495
| 75. Sort Colors | [Link](https://leetcode.com/problems/sort-colors/) | [Link](./lib/medium/75_sort_colors.rb) |
496496
| 78. Subsets | [Link](https://leetcode.com/problems/subsets/) | [Link](./lib/medium/78_subsets.rb) |
497+
| 82. Remove Duplicates from Sorted List II | [Link](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [Link](./lib/medium/82_remove_duplicates_from_sorted_list_ii.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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 = '6.2.8'
8+
s.version = '6.2.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
6+
# @param {ListNode} head
7+
# @return {ListNode}
8+
def delete_duplicates82(head)
9+
result = ::ListNode.new(0, head)
10+
prev = result
11+
12+
until head.nil?
13+
if !head.next.nil? && head.val == head.next.val
14+
head = head.next while !head.next.nil? && head.val == head.next.val
15+
16+
prev.next = head.next
17+
else
18+
prev = prev.next
19+
end
20+
21+
head = head.next
22+
end
23+
24+
result.next
25+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/82_remove_duplicates_from_sorted_list_ii'
6+
require 'minitest/autorun'
7+
8+
class RemoveDuplicatesFromSortedListIITest < ::Minitest::Test
9+
def test_default
10+
assert(
11+
::ListNode.are_equals(
12+
::ListNode.from_array(
13+
[1, 2, 5]
14+
),
15+
delete_duplicates82(
16+
::ListNode.from_array(
17+
[1, 2, 3, 3, 4, 4, 5]
18+
)
19+
)
20+
)
21+
)
22+
assert(
23+
::ListNode.are_equals(
24+
::ListNode.from_array([2, 3]),
25+
delete_duplicates82(
26+
::ListNode.from_array(
27+
[1, 1, 1, 2, 3]
28+
)
29+
)
30+
)
31+
)
32+
end
33+
end

0 commit comments

Comments
 (0)