-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206.2_reverse_linked_list.rb
65 lines (51 loc) · 1.38 KB
/
206.2_reverse_linked_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
require_relative '../test_helper'
# https://leetcode.com/problems/reverse-linked-list
# Runtime: 84 ms, faster than 80.18% of Ruby online submissions for Reverse Linked List.
# Memory Usage: 211.3 MB, less than 23.72% of Ruby online submissions for Reverse Linked List.
class ListNode
attr_accessor :val, :next
def initialize(val = 0, _next = nil)
@val = val
@next = _next
end
end
class LinkedList
def reverse_list(head = nil)
return head if head.nil? || head.next.nil?
previous_node = nil
current_node = head
while current_node
next_node = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next_node
end
previous_node
end
end
class TestLinkedList < Minitest::Test
def test_1
i5 = ListNode.new(5)
i4 = ListNode.new(4, i5)
i3 = ListNode.new(3, i4)
i2 = ListNode.new(2, i3)
i1 = ListNode.new(1, i2)
assert_equal i5, LinkedList.new.reverse_list(i1)
assert_equal i4, i5.next
assert_equal i3, i4.next
assert_equal i2, i3.next
assert_equal i1, i2.next
assert_nil i1.next
end
def test_2
i2 = ListNode.new(2)
i1 = ListNode.new(1, i2)
assert_equal i2, LinkedList.new.reverse_list(i1)
assert_equal i1, i2.next
assert_nil i1.next
end
def test_3
assert_nil LinkedList.new.reverse_list
end
end