Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2816: Double a Number Represented as a Linked List

In this guide, we solve Leetcode #2816 Double a Number Represented as a Linked List in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

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.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Stack, Linked List, Math

Intuition

The problem has a natural nested or last-in-first-out structure.

A stack lets us resolve matches in the correct order as we scan.

Approach

Push items as they appear and pop when you can finalize a decision.

The stack captures the unresolved part of the input.

Steps:

  • Push elements as you scan.
  • Pop when a rule or match is satisfied.
  • Use the stack to compute results.

Example

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.

Python Solution

# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]: def reverse(head): dummy = ListNode() cur = head while cur: next = cur.next cur.next = dummy.next dummy.next = cur cur = next return dummy.next head = reverse(head) dummy = cur = ListNode() mul, carry = 2, 0 while head: x = head.val * mul + carry carry = x // 10 cur.next = ListNode(x % 10) cur = cur.next head = head.next if carry: cur.next = ListNode(carry) return reverse(dummy.next)

Complexity

The time complexity is O(n). The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy