2095. Delete the Middle Node of a Linked List Medium
1/**
2 * [2095] Delete the Middle Node of a Linked List
3 *
4 * You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.
5 * The middle node of a linked list of size n is the ⌊n / 2⌋^th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.
6 *
7 * For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
8 *
9 *
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2021/11/16/eg1drawio.png" style="width: 500px; height: 77px;" />
12 * Input: head = [1,3,4,7,1,2,6]
13 * Output: [1,3,4,1,2,6]
14 * Explanation:
15 * The above figure represents the given linked list. The indices of the nodes are written below.
16 * Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
17 * We return the new list after removing this node.
18 *
19 * Example 2:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2021/11/16/eg2drawio.png" style="width: 250px; height: 43px;" />
21 * Input: head = [1,2,3,4]
22 * Output: [1,2,4]
23 * Explanation:
24 * The above figure represents the given linked list.
25 * For n = 4, node 2 with value 3 is the middle node, which is marked in red.
26 *
27 * Example 3:
28 * <img alt="" src="https://assets.leetcode.com/uploads/2021/11/16/eg3drawio.png" style="width: 150px; height: 58px;" />
29 * Input: head = [2,1]
30 * Output: [2]
31 * Explanation:
32 * The above figure represents the given linked list.
33 * For n = 2, node 1 with value 1 is the middle node, which is marked in red.
34 * Node 0 with value 2 is the only node remaining after removing node 1.
35 *
36 * Constraints:
37 *
38 * The number of nodes in the list is in the range [1, 10^5].
39 * 1 <= Node.val <= 10^5
40 *
41 */
42pub struct Solution {}
43use crate::util::linked_list::{ListNode, to_list};
44
45// problem: https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
46// discuss: https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50// Definition for singly-linked list.
51// #[derive(PartialEq, Eq, Clone, Debug)]
52// pub struct ListNode {
53// pub val: i32,
54// pub next: Option<Box<ListNode>>
55// }
56//
57// impl ListNode {
58// #[inline]
59// fn new(val: i32) -> Self {
60// ListNode {
61// next: None,
62// val
63// }
64// }
65// }
66impl Solution {
67 pub fn delete_middle(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
68 Some(Box::new(ListNode::new(0)))
69 }
70}
71
72// submission codes end
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn test_2095() {
80 }
81}
82
Back
© 2025 bowen.ge All Rights Reserved.