82. Remove Duplicates from Sorted List II Medium

@problem@discussion
#Linked List#Two Pointers



1/**
2 * [82] Remove Duplicates from Sorted List II
3 *
4 * Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
5 *  
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg" style="width: 500px; height: 142px;" />
8 * Input: head = [1,2,3,3,4,4,5]
9 * Output: [1,2,5]
10 * 
11 * Example 2:
12 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg" style="width: 500px; height: 205px;" />
13 * Input: head = [1,1,1,2,3]
14 * Output: [2,3]
15 * 
16 *  
17 * Constraints:
18 * 
19 * 	The number of nodes in the list is in the range [0, 300].
20 * 	-100 <= Node.val <= 100
21 * 	The list is guaranteed to be sorted in ascending order.
22 * 
23 */
24pub struct Solution {}
25use crate::util::linked_list::{ListNode, to_list};
26
27// problem: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
28// discuss: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/discuss/?currentPage=1&orderBy=most_votes&query=
29
30// submission codes start here
31
32// Definition for singly-linked list.
33// #[derive(PartialEq, Eq, Clone, Debug)]
34// pub struct ListNode {
35//   pub val: i32,
36//   pub next: Option<Box<ListNode>>
37// }
38// 
39// impl ListNode {
40//   #[inline]
41//   fn new(val: i32) -> Self {
42//     ListNode {
43//       next: None,
44//       val
45//     }
46//   }
47// }
48impl Solution {
49    pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
50        Some(Box::new(ListNode::new(0)))
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_82() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.