2816. Double a Number Represented as a Linked List Medium
1/**
2 * [2816] Double a Number Represented as a Linked List
3 *
4 * You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.
5 * Return the head of the linked list after doubling it.
6 *
7 * <strong class="example">Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
9 * Input: head = [1,8,9]
10 * Output: [3,7,8]
11 * Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
12 *
13 * <strong class="example">Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
15 * Input: head = [9,9,9]
16 * Output: [1,9,9,8]
17 * Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
18 *
19 *
20 * Constraints:
21 *
22 * The number of nodes in the list is in the range [1, 10^4]
23 * <font face="monospace">0 <= Node.val <= 9</font>
24 * The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself.
25 *
26 */
27pub struct Solution {}
28use crate::util::linked_list::{ListNode, to_list};
29
30// problem: https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/
31// discuss: https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35// Definition for singly-linked list.
36// #[derive(PartialEq, Eq, Clone, Debug)]
37// pub struct ListNode {
38// pub val: i32,
39// pub next: Option<Box<ListNode>>
40// }
41//
42// impl ListNode {
43// #[inline]
44// fn new(val: i32) -> Self {
45// ListNode {
46// next: None,
47// val
48// }
49// }
50// }
51impl Solution {
52 pub fn double_it(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
53 Some(Box::new(ListNode::new(0)))
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2816() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.