Skip to content

Commit ad71482

Browse files
authored
2024-12-26 v. 7.5.1: added "725. Split Linked List in Parts"
2 parents 3bb70a9 + 1451f3c commit ad71482

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
615615
| 707. Design Linked List | [Link](https://leetcode.com/problems/design-linked-list/) | [Link](./lib/medium/707_design_linked_list.rb) | [Link](./test/medium/test_707_design_linked_list.rb) |
616616
| 713. Subarray Product Less Than K | [Link](https://leetcode.com/problems/subarray-product-less-than-k/) | [Link](./lib/medium/713_subarray_product_less_than_k.rb) | [Link](./test/medium/test_713_subarray_product_less_than_k.rb) |
617617
| 720. Longest Word in Dictionary | [Link](https://leetcode.com/problems/longest-word-in-dictionary/) | [Link](./lib/medium/720_longest_word_in_dictionary.rb) | [Link](./test/medium/test_720_longest_word_in_dictionary.rb) |
618+
| 725. Split Linked List in Parts | [Link](https://leetcode.com/problems/split-linked-list-in-parts/) | [Link](./lib/medium/725_split_linked_list_in_parts.rb) | [Link](./test/medium/test_725_split_linked_list_in_parts.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 = '7.5.0'
8+
s.version = '7.5.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/common/linked_list.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def self.from_array(values)
3030
# @return {Boolean}
3131
def self.are_equals(first, second)
3232
return true if first.nil? && second.nil?
33+
3334
return false if first.nil? || second.nil?
3435

3536
first.val == second.val && are_equals(first.next, second.next)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/split-linked-list-in-parts/
6+
# @param {ListNode} head
7+
# @param {Integer} k
8+
# @return {ListNode[]}
9+
def split_list_to_parts(head, k)
10+
p = head
11+
count = 0
12+
13+
until p.nil?
14+
p = p.next
15+
count += 1
16+
end
17+
18+
part = count / k
19+
remainder = count % k
20+
result = []
21+
p = head
22+
23+
(0...k).each do |i|
24+
unit = ::ListNode.new(0)
25+
curr = unit
26+
27+
(0...(part + (i < remainder ? 1 : 0))).each do |_|
28+
next unless p
29+
30+
curr = curr.next = ::ListNode.new(p.val)
31+
p = p.next
32+
end
33+
34+
result << unit.next
35+
end
36+
37+
result
38+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/725_split_linked_list_in_parts'
6+
require 'minitest/autorun'
7+
8+
class SplitLinkedListInPartsTest < ::Minitest::Test
9+
def test_default_one
10+
result = split_list_to_parts(
11+
::ListNode.from_array(
12+
[1, 2, 3]
13+
),
14+
5
15+
)
16+
17+
assert_equal(5, result.size)
18+
19+
[
20+
::ListNode.from_array([1]),
21+
::ListNode.from_array([2]),
22+
::ListNode.from_array([3]),
23+
::ListNode.from_array([]),
24+
::ListNode.from_array([])
25+
].each_with_index do |list, index|
26+
assert(
27+
::ListNode.are_equals(
28+
list,
29+
result[index]
30+
)
31+
)
32+
end
33+
end
34+
35+
def test_default_two
36+
result = split_list_to_parts(
37+
::ListNode.from_array(
38+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
39+
),
40+
3
41+
)
42+
43+
assert_equal(3, result.size)
44+
45+
[
46+
::ListNode.from_array(
47+
[1, 2, 3, 4]
48+
),
49+
::ListNode.from_array(
50+
[5, 6, 7]
51+
),
52+
::ListNode.from_array(
53+
[8, 9, 10]
54+
)
55+
].each_with_index do |list, index|
56+
assert(
57+
::ListNode.are_equals(
58+
list,
59+
result[index]
60+
)
61+
)
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)