File tree 4 files changed +92
-1
lines changed
4 files changed +92
-1
lines changed Original file line number Diff line number Diff line change @@ -521,3 +521,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
521
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 ) |
522
522
| 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 ) |
523
523
| 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 ) |
524
+ | 148. Sort List | [ Link] ( https://leetcode.com/problems/sort-list/ ) | [ Link] ( ./lib/medium/148_sort_list.rb ) | [ Link] ( ./test/medium/test_148_sort_list.rb ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.5.5.1 '
8
+ s . version = '6.5.6 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # https://leetcode.com/problems/sort-list/
4
+ # @param {ListNode} head
5
+ # @return {ListNode}
6
+ def sort_list ( head )
7
+ return head if head . nil? || head . next . nil?
8
+
9
+ temp = head
10
+ slow = head
11
+ fast = head
12
+
13
+ while fast &.next
14
+ temp = slow
15
+ slow = slow . next
16
+ fast = fast . next . next
17
+ end
18
+
19
+ temp . next = nil
20
+
21
+ left = sort_list ( head )
22
+ right = sort_list ( slow )
23
+
24
+ merge_nodes ( left , right )
25
+ end
26
+
27
+ private
28
+
29
+ # @param {ListNode} left
30
+ # @param {ListNode} right
31
+ # @return {ListNode}
32
+ def merge_nodes ( left , right )
33
+ temp = ::ListNode . new ( 0 )
34
+ curr = temp
35
+
36
+ while left && right
37
+ if left . val < right . val
38
+ curr . next = left
39
+ left = left . next
40
+ else
41
+ curr . next = right
42
+ right = right . next
43
+ end
44
+
45
+ curr = curr . next
46
+ end
47
+
48
+ curr . next = left if left
49
+ curr . next = right if right
50
+
51
+ temp . next
52
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+ require_relative '../../lib/common/linked_list'
5
+ require_relative '../../lib/medium/148_sort_list'
6
+ require 'minitest/autorun'
7
+
8
+ class SortListTest < ::Minitest ::Test
9
+ def test_default_one
10
+ assert (
11
+ ::ListNode . are_equals (
12
+ ::ListNode . from_array (
13
+ [ 1 , 2 , 3 , 4 ]
14
+ ) ,
15
+ sort_list (
16
+ ::ListNode . from_array (
17
+ [ 4 , 2 , 1 , 3 ]
18
+ )
19
+ )
20
+ )
21
+ )
22
+ end
23
+
24
+ def test_default_two
25
+ assert (
26
+ ::ListNode . are_equals (
27
+ ::ListNode . from_array (
28
+ [ -1 , 0 , 3 , 4 , 5 ]
29
+ ) ,
30
+ sort_list (
31
+ ::ListNode . from_array (
32
+ [ -1 , 5 , 3 , 4 , 0 ]
33
+ )
34
+ )
35
+ )
36
+ )
37
+ end
38
+ end
You can’t perform that action at this time.
0 commit comments