Skip to content

2024-07-24 v. 6.3.0: added "86. Partition List" #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 75. Sort Colors | [Link](https://leetcode.com/problems/sort-colors/) | [Link](./lib/medium/75_sort_colors.rb) |
| 78. Subsets | [Link](https://leetcode.com/problems/subsets/) | [Link](./lib/medium/78_subsets.rb) |
| 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) |
| 86. Partition List | [Link](https://leetcode.com/problems/partition-list/) | [Link](./lib/medium/86_partition_list.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.2.9'
s.version = '6.3.0'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
30 changes: 30 additions & 0 deletions lib/medium/86_partition_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require_relative '../common/linked_list'

# https://leetcode.com/problems/partition-list/
# @param {ListNode} head
# @param {Integer} x
# @return {ListNode}
def partition(head, x)
f = ::ListNode.new
f_p = f
s = ::ListNode.new
s_p = s

until head.nil?
if head.val < x
f_p.next = ::ListNode.new(head.val)
f_p = f_p.next
else
s_p.next = ::ListNode.new(head.val)
s_p = s_p.next
end

head = head.next
end

f_p&.next = s.next

f.next
end
33 changes: 33 additions & 0 deletions test/medium/test_86_partition_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/linked_list'
require_relative '../../lib/medium/86_partition_list'
require 'minitest/autorun'

class PartitionListTest < ::Minitest::Test
def test_default
assert(
::ListNode.are_equals(
::ListNode.from_array(
[1, 2, 2, 4, 3, 5]
),
partition(
::ListNode.from_array(
[1, 4, 3, 2, 5, 2]
),
3
)
)
)
assert(
::ListNode.are_equals(
::ListNode.from_array([1, 2]),
partition(
::ListNode.from_array([2, 1]),
2
)
)
)
end
end
Loading