問題
https://leetcode.com/problems/merge-two-sorted-lists/
解いてみた
# Definition for singly-linked list. # class ListNode # attr_accessor :val, :next # def initialize(val = 0, _next = nil) # @val = val # @next = _next # end # end # @param {ListNode} list1 # @param {ListNode} list2 # @return {ListNode} def get_list(list_node) result = [] return result if list_node.nil? result << list_node.val loop do next_node = list_node.next if next_node.nil? break end result << next_node.val list_node = next_node end result end def merge_two_lists(list1, list2) l1 = get_list(list1) l2 = get_list(list2) (l1 + l2).sort end
結果