2.两数之和

题目:

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

这道题有点坑,英文题目是返回一个链表即可,不一定是新的链表:

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

但是中文要求返回一个新的链表,这里面生成链表需要消耗部分资源。

思路:

 题目链表是逆序存储有个优势,就是可以直接从头部相加,头部都是各位。

这个题很多思路:

扫描二维码关注公众号,回复: 2597654 查看本文章

1:列表转数字,数字相加,数字转列表

2:直接在列表上就地操作,设置当前头部指针,最多生成一个节点,放到最高处的进位,然后新列表是这个两个列表组合而成

猜你喜欢

转载自www.cnblogs.com/jingshanjunmoxiao/p/9435235.html
今日推荐