题目:
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
解答:
此问题是一个简单的链表的遍历问题,难度不大,注意进位的处理即可。
首先面临的问题是如何在链表尾部添加一个新结点,其实考虑到链表本身结构的特殊性,用递归的方法添加新结点是最简单的方法。
接下来用一个循环同时遍历输入的两个链表,直到两个链表都已到达尾部并且进位为0时停止。
本文介绍了一个简单的链表遍历问题,即如何将两个表示非负数的链表相加并返回新的链表。通过递归地在链表尾部添加新节点,并利用循环遍历两个输入链表直至到达尾部且没有进位。
3223

被折叠的 条评论
为什么被折叠?



