Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Add two numbers represented by linked lists

Add two non-negative integers represented by linked lists where each node stores a single digit in reverse order.

Python practice24 minLinked ListsAdvancedLast updated March 27, 2026

Problem statement

You are given two non-empty singly linked lists representing two non-negative integers. The digits are stored in reverse order such that the 1's digit is at the head of the list. Each node contains a single digit. Add the two numbers and return the sum as a linked list in the same reversed format. You may assume the lists represent numbers without leading zeros, except the number 0 itself. You should handle lists of different lengths and carry propagation. The function should return the head of the new linked list representing the sum.

Task

Implement addition of two numbers represented by linked lists (digits stored in reverse order) and return the sum as a linked list in the same format.

Examples

Simple addition with carry

Input

[2,4,3] + [5,6,4]

Output

[7,0,8]

342 + 465 = 807; reversed representation is [7,0,8].

Input format

Two linked lists are built from Python lists using build_list helper: add_two_numbers(build_list([digits...]), build_list([digits...]))

Output format

Return the head of the summed linked list. Tests convert it to a Python list using list_to_py(head).

Constraints

- Nodes count up to 10^4 for reasoning; tests use practical sizes. - Digits are 0-9. - You must correctly propagate carries across the full length.

Samples

Sample 1

Input

[0] + [0]

Output

[0]

0 + 0 = 0