Talentd
FleetCode

2816. Double a Number Represented as a Linked List

Medium61.4% acceptance3 companies

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.

Asked at 3 companies

Hints open one at a time

Hint 1
Traverse the linked list from the least significant digit to the most significant digit and multiply each node's value by 2
Hint 2
Handle any carry-over digits that may arise during the doubling process.
Hint 3
If there is a carry-over digit on the most significant digit, create a new node with that value and point it to the start of the given linked list and return it.
43:12
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, val=0, next=None):
4# self.val = val
5# self.next = next
6class Solution:
7 def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
8
Starter codePython 3Spaces: 4
Examples
Input
[1,8,9]
Input
[9,9,9]
Your code is saved on this device.Solution