Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Add Two Numbers in a Linked List

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

Your question is Add Two Numbers in a Linked List. 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

Ciena network software may represent large numeric counters as digit-by-digit linked structures. Given two non-empty linked lists, add the numbers they represent and return the result as a linked list.

Each list stores digits in reverse order: the head contains the ones digit, and each following node contains the next digit. Each node contains one digit from 0 through 9. The result must use the same reverse-order representation. Create a new node for every result digit, and preserve a final carry when one remains.

The platform provides a ListNode class with val and next fields. The function receives the heads of the two lists and returns the head of a newly constructed result list.

Constraints

  • 1 <= len(l1), len(l2) <= 100
  • 0 <= node.val <= 9
  • The input lists are acyclic
  • The input lists represent non-negative integers

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