问题:
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single
digit.
Add the two numbers and return it as a linked list.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
官方难度:
Medium
翻译:
现有2个链表结构,每个节点存储非负整数,每个结点的数字都是个位数且倒序排列,将这2个链表代表的数字相加。
例子:
链表一:2 -> 4 -> 3
链表二:5 -> 6 -> 4
输出链表:7 -> 0 -> 8
额外例子:
链表一:1 -> 5
链表二:8 -> 5
输出链表:9 -> 0 -> 1
- 结点定义已经给了,是单向链表的结点定义:
ListNode
1 private static class ListNode { 2 int val; 3 ListNode next; 4 5 ListNode(int x) { 6 val = x; 7 } 8 }