-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-nodes-in-between-zeros.rs
45 lines (43 loc) · 1.27 KB
/
merge-nodes-in-between-zeros.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
// https://leetcode.com/problems/merge-nodes-in-between-zeros/
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut result: Option<Box<ListNode>> = None;
let mut sum = 0;
let mut current_node = &head;
let mut cur_result_node = &mut result;
while let Some(node) = current_node{
sum += node.val;
if let Some(next) = node.next.as_ref() {
if next.val == 0 {
let new = Some(Box::new(ListNode::new(sum)));
if let Some(ref mut cur) = cur_result_node {
cur.next = new;
cur_result_node = &mut cur.next;
} else {
result = new;
cur_result_node = &mut result;
}
sum = 0;
}
}
current_node = &node.next;
}
result
}
}