euisblue
148. Sort List

Ruby

1# runtime: 116ms faster than 100% of Ruby submission 2# memory: 217.1MB, less than 80% of Ruby submission 3 4# Definition for singly-linked list. 5# class ListNode 6# attr_accessor :val, :next 7# def initialize(val = 0, _next = nil) 8# @val = val 9# @next = _next 10# end 11# end 12# @param {ListNode} head 13# @return {ListNode} 14def sort_list(head) 15 nodes = [] 16 curr = head 17 while curr 18 nodes << curr.val 19 curr = curr.next 20 end 21 22 nodes.sort! 23 curr = head 24 for i in 0...nodes.size 25 curr.val = nodes[i] 26 curr = curr.next 27 end 28 29 head 30end