TypeScript算法:两数相加
发布时间
阅读量:
阅读量
TypeScript算法-2. 两数相加
- 理念
- 程序
思路
两个数值均以逆序方式通过链表结构进行连接,这恰好对应了我们在执行加法运算时,从最低位向最高位逐位处理数对的流程。在编写相关程序时,需要特别注意对当前进位值的维护与处理。
代码
/** * Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
let carry = 0; // 进位
let
全部评论 (0)
还没有任何评论哟~
