leetCode练习(2)

来源:互联网 发布:优化软件下载 编辑:程序博客网 时间:2024/04/29 22:01
题目:Add Two Numbers           
难度:medium         
问题描述:

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

错误点:分别计算出l1和l2代表的int值再相加,这是错误解法,因为int会溢出。

解题思路:每位相加注意进位即可。代码如下:


0 0
原创粉丝点击