Double a Number Represented as a Linked List - Complete Solution Guide
Double a Number Represented as a Linked List is LeetCode problem 2816, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem requires you to double a non-negative integer represented as a singly-linked list. Each node in the linked list contains a single digit of the number. The goal is to modify the linked list in-place (if possible or create a new one if necessary) to represent the doubled value of the original number. The input linked list represents a number without leading zeros, except in the case where the number itself is zero. The output should be the head of the modified linked list.
Solution Approach
The solution simulates the doubling process with carry-over propagation. It first checks if the most significant digit (head node's value) is greater than 4. If it is, this means after doubling, we'll have a carry and a new node with value 1 needs to be added at the beginning. The algorithm then iterates through the linked list, doubling each node's value and handling carry-overs. The trick here is to use the value of the next node to know whether to carry over a 1 when doubling the current node's value. If the next node's value is greater than 4, a carry-over will occur from the next node to the current node. The doubled value modulo 10 gives the digit at that node.
Step-by-Step Algorithm
- Step 1: Check if the head's value is greater than 4. If it is, create a new node with value 0 and make it the new head, pointing to the original head.
- Step 2: Initialize a current pointer to the head of the linked list.
- Step 3: Iterate through the linked list using the current pointer. While current is not null:
- Step 4: Double the value of the current node.
- Step 5: If the current node has a next node and its value is greater than 4, add 1 to the doubled value (carry-over).
- Step 6: Update the current node's value to doubled_value % 10.
- Step 7: Move the current pointer to the next node.
- Step 8: Return the head of the linked list.
Key Insights
- Insight 1: Doubling the number is analogous to multiplying it by 2. We need to handle potential carry-overs from right to left, similar to manual multiplication.
- Insight 2: Since we're dealing with a linked list, we can't easily traverse from right to left. The provided solution cleverly uses the node values themselves to predict and propagate carries from left to right. A node's value greater than 4 means when doubled, the next digit will have a carry-over.
- Insight 3: If the first digit is greater than 4, we'll have a carry-over in the most significant digit, requiring us to create a new head node with value 1 (after the carry-over calculation).
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Linked List, Math, Stack.
Companies
Asked at: Nvidia.