Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List: Add Two Numbers

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Linked List: Add Two Numbers. Start with the requirements on the right.

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.

Problem

Lytx may represent large event-count values as linked lists to avoid fixed-width integer limits. Given two non-negative integers stored in reverse digit order, return their sum in the same linked-list format.

Each node contains one decimal digit. The head node stores the least significant digit. The lists may have different lengths, and the result must not contain unnecessary leading zero nodes, except when the sum is zero.

Formal Specification

Implement add_two_numbers(l1, l2), where l1 and l2 are heads of singly linked lists whose node values are integers from 0 through 9. Return the head of a new singly linked list representing l1 + l2 in reverse digit order. The provided environment defines ListNode with fields val and next.

For test cases, each linked list is written as a JSON array for readability, and the evaluator converts it to a linked list before calling the function. The expected result is written as an array of node values.

Constraints

  • 1 <= len(l1), len(l2) <= 100
  • Each node value is an integer from 0 through 9
  • The input lists represent non-negative integers
  • Input lists may have different lengths

Function Signature

def add_two_numbers(l1, l2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output