Skip to content

Commit 543fbdc

Browse files
authoredAug 22, 2024··
Merge pull request #718 from fartem/143_Reorder_List
2024-08-22 v. 6.5.3: added "143. Reorder List"
2 parents 26b878a + f173da2 commit 543fbdc

12 files changed

+115
-13
lines changed
 

‎.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Style/MethodCalledOnDoEndBlock:
1818
Enabled: false
1919
Style/MultilineMethodSignature:
2020
Enabled: false
21+
Style/DisableCopsWithinSourceCodeDirective:
22+
Enabled: false
2123

2224
Bundler/GemComment:
2325
Enabled: false

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
518518
| 134. Gas Station | [Link](https://leetcode.com/problems/gas-station/) | [Link](./lib/medium/134_gas_station.rb) | [Link](./test/medium/test_134_gas_station.rb) |
519519
| 138. Copy List with Random Pointer | [Link](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Link](./lib/medium/138_copy_list_with_random_pointer.rb) | [Link](./test/medium/test_138_copy_list_with_random_pointer.rb) |
520520
| 142. Linked List Cycle II | [Link](https://leetcode.com/problems/linked-list-cycle-ii/) | [Link](./lib/medium/142_linked_list_cycle_ii.rb) | [Link](./test/medium/test_142_linked_list_cycle_ii.rb) |
521+
| 143. Reorder List | [Link](https://leetcode.com/problems/reorder-list/) | [Link](./lib/medium/143_reorder_list.rb) | [Link](./test/medium/test_143_reorder_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.5.2.1'
8+
s.version = '6.5.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

‎lib/easy/2243_calculate_digit_sum_of_a_string.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rubocop:disable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
1+
# rubocop:disable Style/FrozenStringLiteralComment
22

33
# https://leetcode.com/problems/calculate-digit-sum-of-a-string/
44
# @param {String} s
@@ -21,4 +21,4 @@ def digit_sum(s, k)
2121
s
2222
end
2323

24-
# rubocop:enable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
24+
# rubocop:enable Style/FrozenStringLiteralComment

‎lib/easy/2259_remove_digit_from_number_to_maximize_result.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rubocop:disable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
1+
# rubocop:disable Style/FrozenStringLiteralComment
22

33
# https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/
44
# @param {String} number
@@ -18,4 +18,4 @@ def remove_digit(number, digit)
1818
result
1919
end
2020

21-
# rubocop:enable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
21+
# rubocop:enable Style/FrozenStringLiteralComment

‎lib/easy/234_palindrome_linked_list.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def is_palindrome_ll(head)
2323
true
2424
end
2525

26+
private
27+
2628
# @param {ListNode} node
2729
# @return {ListNode}
2830
def reverse(node)

‎lib/medium/143_reorder_list.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/reorder-list/
6+
# @param {ListNode} head
7+
# @return {Void} Do not return anything, modify head in-place instead.
8+
def reorder_list(head)
9+
tort = head
10+
hare = head.next
11+
while hare&.next
12+
tort = tort.next
13+
hare = hare.next.next
14+
end
15+
16+
f_part = head
17+
s_part = tort.next
18+
tort.next = nil
19+
s_part = reverse_in_reorder(s_part)
20+
21+
temp = ::ListNode.new
22+
curr = temp
23+
while f_part || s_part
24+
if f_part
25+
curr.next = f_part
26+
curr = curr.next
27+
f_part = f_part.next
28+
end
29+
30+
next unless s_part
31+
32+
curr.next = s_part
33+
curr = curr.next
34+
s_part = s_part.next
35+
end
36+
37+
# rubocop:disable Lint/UselessAssignment
38+
head = temp.next
39+
# rubocop:enable Lint/UselessAssignment
40+
end
41+
42+
private
43+
44+
# @param {ListNode} head
45+
# @return {ListNode}
46+
def reverse_in_reorder(head)
47+
prev = nil
48+
curr = head
49+
until curr.nil?
50+
nxt = curr.next
51+
curr.next = prev
52+
prev = curr
53+
curr = nxt
54+
end
55+
56+
prev
57+
end

‎test/easy/test_1108_defanging_an_ip_address.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'minitest/autorun'
66

77
class DefangingAnIPAddressTest < ::Minitest::Test
8-
# rubocop:disable Style/DisableCopsWithinSourceCodeDirective, Style/IpAddresses
8+
# rubocop:disable Style/IpAddresses
99
def test_default_one
1010
assert_equal(
1111
'1[.]1[.]1[.]1',
@@ -19,5 +19,5 @@ def test_default_two
1919
defang_i_paddr('255.100.50.0')
2020
)
2121
end
22-
# rubocop:enable Style/DisableCopsWithinSourceCodeDirective, Style/IpAddresses
22+
# rubocop:enable Style/IpAddresses
2323
end

‎test/easy/test_1619_mean_of_array_after_removing_some_elements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'minitest/autorun'
66

77
class MeanOfArrayAfterMovingSomeElementsTest < ::Minitest::Test
8-
# rubocop:disable Style/DisableCopsWithinSourceCodeDirective, Layout/MultilineArrayLineBreaks
8+
# rubocop:disable Layout/MultilineArrayLineBreaks
99
def test_default_one
1010
assert_equal(
1111
2.00000,
@@ -44,5 +44,5 @@ def test_default_three
4444
)
4545
end
4646

47-
# rubocop:enable Style/DisableCopsWithinSourceCodeDirective, Layout/MultilineArrayLineBreaks
47+
# rubocop:enable Layout/MultilineArrayLineBreaks
4848
end

‎test/easy/test_2243_calculate_digit_sum_of_a_string.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rubocop:disable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
1+
# rubocop:disable Style/FrozenStringLiteralComment
22

33
require_relative '../test_helper'
44
require_relative '../../lib/easy/2243_calculate_digit_sum_of_a_string'
@@ -10,4 +10,4 @@ def test_default_one = assert_equal('135', digit_sum('11111222223', 3))
1010
def test_default_two = assert_equal('000', digit_sum('00000000', 3))
1111
end
1212

13-
# rubocop:enable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
13+
# rubocop:enable Style/FrozenStringLiteralComment

‎test/easy/test_2259_remove_digit_from_number_to_maximize_result.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rubocop:disable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
1+
# rubocop:disable Style/FrozenStringLiteralComment
22

33
require_relative '../test_helper'
44
require_relative '../../lib/easy/2259_remove_digit_from_number_to_maximize_result'
@@ -12,4 +12,4 @@ def test_default_two = assert_equal('231', remove_digit('1231', '1'))
1212
def test_default_three = assert_equal('51', remove_digit('551', '5'))
1313
end
1414

15-
# rubocop:enable Style/FrozenStringLiteralComment, Style/DisableCopsWithinSourceCodeDirective
15+
# rubocop:enable Style/FrozenStringLiteralComment

‎test/medium/test_143_reorder_list.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/143_reorder_list'
6+
require 'minitest/autorun'
7+
8+
class ReorderListTest < ::Minitest::Test
9+
def test_default_one
10+
input = ::ListNode.from_array(
11+
[1, 2, 3, 4]
12+
)
13+
reorder_list(input)
14+
15+
assert(
16+
::ListNode.are_equals(
17+
input,
18+
::ListNode.from_array(
19+
[1, 4, 2, 3]
20+
)
21+
)
22+
)
23+
end
24+
25+
def test_default_two
26+
input = ::ListNode.from_array(
27+
[1, 2, 3, 4, 5]
28+
)
29+
reorder_list(input)
30+
31+
assert(
32+
::ListNode.are_equals(
33+
input,
34+
::ListNode.from_array(
35+
[1, 5, 2, 4, 3]
36+
)
37+
)
38+
)
39+
end
40+
end

0 commit comments

Comments
 (0)
Please sign in to comment.