-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143. Reorder List.rb
More file actions
56 lines (47 loc) · 1.03 KB
/
143. Reorder List.rb
File metadata and controls
56 lines (47 loc) · 1.03 KB
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
def reorder_list(head)
arr = []
while head!=nil
arr.push(head)
head = head.next
end
# arr.each {|a| pp a.val}
result = []
for i in 0..(arr.length-1)/2
result.push(arr[i])
result.push(arr[arr.length-1-i])
end
for i in 1..result.length
result[i-1].next = result[i]
end
result[0]
end
def reorder_list(head)
arr = []
while head!=nil
arr.push(head)
head = head.next
end
# result = []
for i in 0..(arr.length-1)/2
arr[i].next = arr[arr.length-1-i]
if i > 0
arr[arr.length-1-(i-1)].next = arr[i]
end
end
arr[arr.length/2].next=nil
arr[0]
end
def reorder_list(head)
arr = []
while head!=nil
arr.push(head)
head = head.next
end
arr[0].next = arr[arr.length-1-0]
for i in 1..(arr.length-1)/2
arr[i].next = arr[arr.length-1-i]
arr[arr.length-1-(i-1)].next = arr[i]
end
arr[arr.length/2].next=nil
arr[0]
end