LeetCode-Add Two Numbers II
来源:互联网 发布:2017年中国m2数据 编辑:程序博客网 时间:2024/05/28 01:36
Description
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
Solution
思路:
这里使用了两个栈将两个链表的值反置;然后一次按位相加即可得到和。
这里有几个点需要注意下:
1. 相加后得到的数据是从低位开始的。需要进行反置(或者直接在返回值上做文章)
2. 按位相加得到和后,需要注意下最后是否还需要高位进一;如果需要则需要补充一个高位。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Stack<Integer> sta1 = new Stack<Integer>(); Stack<Integer> sta2 = new Stack<Integer>(); while (l1 != null) { sta1.push(l1.val); l1 = l1.next; } while (l2 != null) { sta2.push(l2.val); l2 = l2.next; } ListNode re = null; int x = 0; while (!sta1.isEmpty() || !sta2.isEmpty()) { x += (sta1.isEmpty() ? 0 : sta1.pop()) + (sta2.isEmpty() ? 0 : sta2.pop()); ListNode node = new ListNode(x%10); node.next = re; re = node; x = x / 10; } if(x ==1){ ListNode node = new ListNode(1); node.next = re; re = node; } return re; }}
阅读全文
1 0
- [LeetCode]Add Two Numbers II
- Add Two Numbers II ---LeetCode
- Leetcode Add Two Numbers II
- LeetCode-Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- leetcode-445. Add Two Numbers II
- Leetcode Add Two Numbers II 445
- LeetCode 445. Add Two Numbers II
- [LeetCode]445. Add Two Numbers II
- leetcode 445 Add Two Numbers II C++
- LeetCode 445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- 【LeetCode】 445. Add Two Numbers II
- LeetCode笔记:445. Add Two Numbers II
- leetcode 445. Add Two Numbers II
- 【Leetcode】445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- LeetCode 445. Add Two Numbers II
- hexo搭建博客文章目录分析
- Java提高篇——equals()与hashCode()方法详解
- POJ 2115 Looooops 扩展欧几里德 Java
- STM32外部中断初始化
- 注释转换(C注释转换为c++注释)
- LeetCode-Add Two Numbers II
- 【自考】-信息系统开发管理-第一遍
- python读写文件
- oracle数据库《2》
- 设计模式总述和跳转目录
- 团队管理和leadership的一些感悟
- PKU 1046-Square Number(数论)
- Linux的初级命令行
- Error[e46]: Undefined external "debug_str::?relay" referred