Skip to content

2024-08-26 v. 6.5.5: updated test file for "147. Insertion Sort List" #720

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 2 commits into from
Aug 26, 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 @@ -520,3 +520,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 146. LRU Cache | [Link](https://leetcode.com/problems/lru-cache/) | [Link](./lib/medium/146_lru_cache.rb) | [Link](./test/medium/test_146_lru_cache.rb) |
| 147. Insertion Sort List | [Link](https://leetcode.com/problems/insertion-sort-list/) | [Link](./lib/medium/147_insertion_sort_list.rb) | [Link](./test/medium/test_147_insertion_sort_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.5.4'
s.version = '6.5.5'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
24 changes: 24 additions & 0 deletions lib/medium/147_insertion_sort_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require_relative '../common/linked_list'

# https://leetcode.com/problems/insertion-sort-list/
# @param {ListNode} head
# @return {ListNode}
def insertion_sort_list(head)
result = ::ListNode.new
curr = head

while curr
prev = result

prev = prev.next while prev.next && prev.next.val <= curr.val

nxt = curr.next
curr.next = prev.next
prev.next = curr
curr = nxt
end

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

require_relative '../test_helper'
require_relative '../../lib/common/linked_list'
require_relative '../../lib/medium/147_insertion_sort_list'
require 'minitest/autorun'

class InsertionSortListTest < ::Minitest::Test
def test_default_one
assert(
::ListNode.are_equals(
::ListNode.from_array(
[1, 2, 3, 4]
),
insertion_sort_list(
::ListNode.from_array(
[4, 2, 1, 3]
)
)
)
)
end

def test_default_two
assert(
::ListNode.are_equals(
::ListNode.from_array(
[-1, 0, 3, 4, 5]
),
insertion_sort_list(
::ListNode.from_array(
[-1, 5, 3, 4, 0]
)
)
)
)
end
end
Loading