1171. Remove Zero Sum Consecutive Nodes from Linked List Medium

@problem@discussion
#Hash Table#Linked List



1/**
2 * [1171] Remove Zero Sum Consecutive Nodes from Linked List
3 *
4 * Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.
5 * 
6 * After doing so, return the head of the final linked list.  You may return any such answer.
7 *  
8 * (Note that in the examples below, all sequences are serializations of ListNode objects.)
9 * Example 1:
10 * 
11 * Input: head = [1,2,-3,3,1]
12 * Output: [3,1]
13 * Note: The answer [1,2,1] would also be accepted.
14 * 
15 * Example 2:
16 * 
17 * Input: head = [1,2,3,-3,4]
18 * Output: [1,2,4]
19 * 
20 * Example 3:
21 * 
22 * Input: head = [1,2,3,-3,-2]
23 * Output: [1]
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	The given linked list will contain between 1 and 1000 nodes.
29 * 	Each node in the linked list has -1000 <= node.val <= 1000.
30 * 
31 */
32pub struct Solution {}
33use crate::util::linked_list::{ListNode, to_list};
34
35// problem: https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/
36// discuss: https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40// Definition for singly-linked list.
41// #[derive(PartialEq, Eq, Clone, Debug)]
42// pub struct ListNode {
43//   pub val: i32,
44//   pub next: Option<Box<ListNode>>
45// }
46// 
47// impl ListNode {
48//   #[inline]
49//   fn new(val: i32) -> Self {
50//     ListNode {
51//       next: None,
52//       val
53//     }
54//   }
55// }
56impl Solution {
57    pub fn remove_zero_sum_sublists(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
58        Some(Box::new(ListNode::new(0)))
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_1171() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.