# Definition for singly-linked list. class ListNode attr_accessor :val, :next def initialize(val = 0, _next = nil) @val = val @next = _next end end