Skip to content

Commit 8ab8599

Browse files
committed
2024-07-24 v. 6.3.0: added "86. Partition List"
1 parent f9106f9 commit 8ab8599

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
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) |
497497
| 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) |
498+
| 86. Partition List | [Link](https://leetcode.com/problems/partition-list/) | [Link](./lib/medium/86_partition_list.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.9'
8+
s.version = '6.3.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/86_partition_list.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/partition-list/
6+
# @param {ListNode} head
7+
# @param {Integer} x
8+
# @return {ListNode}
9+
def partition(head, x)
10+
f = ::ListNode.new
11+
f_p = f
12+
s = ::ListNode.new
13+
s_p = s
14+
15+
until head.nil?
16+
if head.val < x
17+
f_p.next = ::ListNode.new(head.val)
18+
f_p = f_p.next
19+
else
20+
s_p.next = ::ListNode.new(head.val)
21+
s_p = s_p.next
22+
end
23+
24+
head = head.next
25+
end
26+
27+
f_p&.next = s.next
28+
29+
f.next
30+
end

test/medium/test_86_partition_list.rb

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/86_partition_list'
6+
require 'minitest/autorun'
7+
8+
class PartitionListTest < ::Minitest::Test
9+
def test_default
10+
assert(
11+
::ListNode.are_equals(
12+
::ListNode.from_array(
13+
[1, 2, 2, 4, 3, 5]
14+
),
15+
partition(
16+
::ListNode.from_array(
17+
[1, 4, 3, 2, 5, 2]
18+
),
19+
3
20+
)
21+
)
22+
)
23+
assert(
24+
::ListNode.are_equals(
25+
::ListNode.from_array([1, 2]),
26+
partition(
27+
::ListNode.from_array([2, 1]),
28+
2
29+
)
30+
)
31+
)
32+
end
33+
end

0 commit comments

Comments
 (0)