Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
LeetCode Add Two Numbers (Linked List)
00:00
5 left

LeetCode Add Two Numbers (Linked List)

EasyPython

Problem

Rivian FleetOS may represent long numeric identifiers as digit streams instead of converting them into fixed-width integers. Given two non-negative integers represented by linked lists, return their sum as a linked list.

Each list stores one decimal digit per node in reverse order, so the head contains the ones digit. Add the numbers digit by digit and propagate any carry to the next position.

Formal Specification

Implement add_two_numbers(l1, l2). l1 and l2 are heads of singly linked lists containing digits from 0 through 9. Return the head of a new singly linked list containing the sum in reverse order. The input lists may have different lengths and must not be modified. For examples and JSON test cases, a list such as [2, 4, 3] represents the linked list 2 -> 4 -> 3, which encodes the number 342.

Constraints

  • 1 <= len(l1), len(l2) <= 100
  • 0 <= node.val <= 9
  • Each input represents a non-negative integer
  • Input lists are provided in reverse digit order
  • Input linked lists must not be modified

Function Signature

def add_two_numbers(l1, l2):
Interviewer

Your question is LeetCode Add Two Numbers (Linked List). Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.