← 返回 bloomberg 的题目列表Add Two Numbers (Forward Order Linked Lists)
类型:online_judge
Problem: Add Two Numbers (Forward Order Linked Lists)
You are given two singly linked lists l1 and l2. Each node contains a single digit (0-9). The digits are stored in forward order, meaning:
The head node is the most significant digit.
The tail node is the least significant digit.
Compute the sum of the two numbers and return it as a linked list in the same forward order.
Example
l1 = 7 -> 2 -> 4 -> 3 represents 7243
l2 = 5 -> 6 -> 4 represents 564
Return: 7 -> 8 -> 0 -> 7 (represents 7807)
Requirements
You must define the ListNode structure yourself.
You must write your own test cases (at least cover: different lengths, carry generation, and an extra carry at the most significant digit).
Follow-up
Solve it without reversing the input lists.
Extra space is allowed.
Constraints (typical interview assumptions)
1 <= len(l1), len(l2) <= 10^4
Node values are 0..9
Implement a function that:
Input: heads l1, l2
Output: head of the summed list
Example
Input
l1: 7 2 4 3
l2: 5 6 4
Output
7 8 0 7