-
Notifications
You must be signed in to change notification settings - Fork 0
/
143_reorder_list.rb
57 lines (47 loc) · 1 KB
/
143_reorder_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
# frozen_string_literal: true
require_relative '../common/linked_list'
# https://leetcode.com/problems/reorder-list/
# @param {ListNode} head
# @return {Void} Do not return anything, modify head in-place instead.
def reorder_list(head)
tort = head
hare = head.next
while hare&.next
tort = tort.next
hare = hare.next.next
end
f_part = head
s_part = tort.next
tort.next = nil
s_part = reverse_in_reorder(s_part)
temp = ::ListNode.new
curr = temp
while f_part || s_part
if f_part
curr.next = f_part
curr = curr.next
f_part = f_part.next
end
next unless s_part
curr.next = s_part
curr = curr.next
s_part = s_part.next
end
# rubocop:disable Lint/UselessAssignment
head = temp.next
# rubocop:enable Lint/UselessAssignment
end
private
# @param {ListNode} head
# @return {ListNode}
def reverse_in_reorder(head)
prev = nil
curr = head
until curr.nil?
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
end
prev
end