Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List Addition Program

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

Your question is Linked List Addition Program. 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

Josh Technology Group QA automation may need to validate arithmetic values represented as linked structures. Given two non-empty singly linked lists, add the numbers they represent and return the sum as a linked list.

Each node stores one decimal digit, and the digits are arranged in reverse order. The first node contains the ones digit. The input lists may have different lengths. Do not convert the lists into integers or strings, and do not modify the input lists.

Formal Specification

Implement add_two_numbers(l1, l2). Each argument is the head of a singly linked list whose nodes contain a val digit and a next pointer. Return the head of a new linked list containing the sum in reverse digit order. The result must not contain unnecessary leading zero nodes, except that zero itself is represented by one node containing 0.

For the examples and test cases, lists are written as arrays for readability. The evaluator converts each array into a linked list before calling the function and converts the returned list back into an array.

Constraints

  • Each list contains 1 to 100,000 nodes
  • 0 <= node.val <= 9
  • The input lists may have different lengths
  • Input lists must not be modified
  • The result uses one node for zero and no unnecessary leading zero nodes

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