-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path086.rb
More file actions
35 lines (30 loc) · 667 Bytes
/
086.rb
File metadata and controls
35 lines (30 loc) · 667 Bytes
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
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end
# @param {ListNode} head
# @param {Integer} x
# @return {ListNode}
def partition(head, x)
values = []
while !head.nil? do
values.push(head.val)
head = head.next
end
part1, part2 = values.partition {|value| value < x}
dummy = ListNode.new("dummy")
current = dummy
part1.each do |value|
current.next = ListNode.new(value)
current = current.next
end
part2.each do |value|
current.next = ListNode.new(value)
current = current.next
end
dummy.next
end