-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_twin_sum_linked_list.rs
71 lines (62 loc) · 1.77 KB
/
maximum_twin_sum_linked_list.rs
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
66
67
68
69
70
71
use std::mem;
#[derive(Debug)]
pub struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
impl ListNode {
fn new(val: i32) -> ListNode {
ListNode { val, next: None }
}
}
fn to_list(vector: Vec<i32>) -> Option<Box<ListNode>> {
let mut cur = None;
for &value in vector.iter().rev() {
let mut new_node = ListNode::new(value);
new_node.next = cur;
cur = Some(Box::new(new_node));
}
cur
}
// ################### Solution start ################################
// get the length of list
fn len(mut head: Option<&Box<ListNode>>) -> usize {
let mut count = 0;
while let Some(node) = head.take() {
count += 1;
head = node.next.as_ref();
}
count
}
// partition list two parts
fn partition_list(mut head: Option<Box<ListNode>>) -> (Option<Box<ListNode>>, Option<Box<ListNode>>) {
let mid = len(head.as_ref()) / 2;
let mut top = None;
for _ in 0..mid {
let mut node = head.take().unwrap();
head = mem::replace(&mut node.next, top.take());
top = Some(node);
}
(top, head)
}
fn pair_sum(head: Option<Box<ListNode>>) -> i32 {
let (mut top, mut bottom) = partition_list(head);
let mut max = i32::MIN;
while let(Some(mut top_node), Some(mut bottom_node)) = (top.take(), bottom.take()) {
top = top_node.next.take();
bottom = bottom_node.next.take();
let sum = top_node.val + bottom_node.val;
if sum > max {
max = sum;
}
}
max
}
// ################### Solution end ################################
fn main() {
//let testcase = vec![5, 4, 2, 1];
//let testcase = vec![4,2,2,3];
let testcase = vec![1, 100000];
let head = to_list(testcase);
println!("Part 1: {}", pair_sum(head));
}