2816. Double a Number Represented as a Linked List LeetCode Solution

In this guide, you will get 2816. Double a Number Represented as a Linked List LeetCode Solution with the best time and space complexity. The solution to Double a Number Represented as a Linked List problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Double a Number Represented as a Linked List solution in C++
  4. Double a Number Represented as a Linked List solution in Java
  5. Double a Number Represented as a Linked List solution in Python
  6. Additional Resources
2816. Double a Number Represented as a Linked List LeetCode Solution image

Problem Statement of Double a Number Represented as a Linked List

You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.
Return the head of the linked list after doubling it.

Example 1:

Input: head = [1,8,9]
Output: [3,7,8]
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.

Example 2:

Input: head = [9,9,9]
Output: [1,9,9,8]
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.

Constraints:

The number of nodes in the list is in the range [1, 104]
0 <= Node.val <= 9
The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)
See also  668. Kth Smallest Number in Multiplication Table LeetCode Solution

2816. Double a Number Represented as a Linked List LeetCode Solution in C++

class Solution {
 public:
  ListNode* doubleIt(ListNode* head) {
    if (getCarry(head) == 1)
      return new ListNode(1, head);
    return head;
  }

 private:
  int getCarry(ListNode* node) {
    int val = node->val * 2;
    if (node->next != nullptr)
      val += getCarry(node->next);

    node->val = val % 10;
    return val / 10;
  }
};
/* code provided by PROGIEZ */

2816. Double a Number Represented as a Linked List LeetCode Solution in Java

class Solution {
  public ListNode doubleIt(ListNode head) {
    if (getCarry(head) == 1)
      return new ListNode(1, head);
    return head;
  }

  private int getCarry(ListNode node) {
    int val = node.val * 2;
    if (node.next != null)
      val += getCarry(node.next);
    node.val = val % 10;
    return val / 10;
  }
}
// code provided by PROGIEZ

2816. Double a Number Represented as a Linked List LeetCode Solution in Python

class Solution:
  def doubleIt(self, head: ListNode | None) -> ListNode | None:
    def getCarry(node: ListNode | None) -> ListNode | None:
      val = node.val * 2
      if node.next:
        val += getCarry(node.next)
      node.val = val % 10
      return val // 10

    if getCarry(head) == 1:
      return ListNode(1, head)
    return head
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.