Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Add Two Numbers as Linked Lists

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

Your question is Add Two Numbers as Linked Lists. 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

Airtel Digital receives numeric values in digit-wise form for some lightweight processing workflows. Given two non-negative integers represented by singly linked lists, return their sum as a singly linked list.

Each node stores one decimal digit, and the digits are arranged in reverse order: the head contains the least significant digit. The two numbers may have different lengths. Do not convert the linked lists into integers or strings.

Formal Specification

Implement add_two_numbers(l1, l2), where l1 and l2 are heads of singly linked lists. Each node has an integer val from 0 to 9 and a next pointer. Return the head of a new linked list containing the sum, also in reverse digit order. The input lists must not be modified.

For test cases, list notation such as [2, 4, 3] represents the linked list 2 -> 4 -> 3.

Constraints

  • 1 <= len(l1), len(l2) <= 100
  • 0 <= node.val <= 9
  • The lists represent non-negative integers.
  • Input lists must not be modified.

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