LeetCode题解(2)--Add Two Numbers
来源:互联网 发布:jquery ajax 提交json 编辑:程序博客网 时间:2024/06/03 20:22
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.
example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8
解题思路
因为整数是逆序存储,所以逐位相加,得到相应的和及进位,若其中一链表循环结束,其值以0代替。
解题代码
C++
#include <iostream>using namespace std;struct ListNode { int val; ListNode* next; ListNode(int x) : val(x),next(NULL) {}};class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode head(0); ListNode* cur = &head; int extra = 0; while (l1 || l2 || extra ) { int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra; extra = sum / 10; cur->next = new ListNode(sum % 10); cur = cur->next; if(l1) l1 = l1->next; if(l2) l2 = l2->next; } return head.next; }};ListNode* createList() { ListNode head(0); ListNode* cur = &head; int x; while(cin >> x){ ListNode* temp = new ListNode(x); cur->next = temp; cur = cur->next; } return head.next;}void printList(ListNode* head) { ListNode* p = head; while(p){ cout << p->val << '\t'; p = p->next; }}int main(){ ListNode* l1 = createList(); cin.clear();//更改cin的状态标示符 cin.sync();//清除缓存区的数据流 ListNode* l2 = createList(); Solution solution; ListNode* head = solution.addTwoNumbers(l1,l2); printList(head); return 0;}
Java
package leetcode;import java.util.Scanner;class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}public class Solution { public static ListNode addTwoNumbers(ListNode l1 , ListNode l2) { ListNode head = new ListNode(0); ListNode cur = head; int extra = 0; while(l1 != null || l2 != null || extra != 0){ int sum = ((l1 != null) ? l1.val : 0) + ((l2 != null) ? l2.val : 0) + extra; extra = sum / 10; cur.next = new ListNode(sum % 10); cur = cur.next; if(l1 != null) l1 = l1.next; if(l2 != null) l2 = l2.next; } return head.next; } public static ListNode createList() { ListNode head = new ListNode(0); ListNode cur = head; Scanner scanner = new Scanner(System.in); while(scanner.hasNextInt()) { int x = scanner.nextInt(); ListNode temp = new ListNode(x); cur.next = temp; cur = cur.next; } return head.next; } public static void printList(ListNode head) { ListNode p = head; while(p != null) { System.out.print(p.val+"\t"); p = p.next; } } public static void main(String[] args) { // TODO Auto-generated method stub ListNode l1 = createList(); ListNode l2 = createList(); ListNode head = addTwoNumbers(l1, l2); printList(head); }}
Python
class ListNode: def __init__(self,x): self.val = x self.next = Noneclass Solution: def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) cur = head extra = 0 while l1 or l2 or extra : sum = (l1.val if l1 else 0) + (l2.val if l2 else 0) + extra extra = sum // 10 temp = ListNode(sum % 10) cur.next = temp cur = cur.next if l1 : l1 = l1.next if l2 : l2 = l2.next return head.nextif __name__ == '__main__' : a , a.next , a.next.next = ListNode(2) , ListNode(4) , ListNode(3) b , b.next , b.next.next = ListNode(5) , ListNode(6) , ListNode(4) solution = Solution() head = solution.addTwoNumbers(a,b) print(head.val,head.next.val,head.next.next.val)
0 0
- LeetCode题解-2-Add Two Numbers
- LeetCode题解(2)--Add Two Numbers
- LeetCode题解 #2 Add Two Numbers
- LeetCode题解:Add Two Numbers
- LeetCode题解:Add Two Numbers
- [LeetCode] Add Two Numbers题解
- LeetCode题解——2Add Two Numbers
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
- LeetCode#2 Add Two Numbers题解(C++版)
- LeetCode 题解(26): Add Two Numbers
- LeetCode题解 || Add Two Numbers 问题
- [Leetcode题解]002 Add Two Numbers
- LeetCode题解--2. Add Two Numbers
- 2. Add Two Numbers : LeetCode 题解
- LeetCode题解:2. Add Two Numbers
- leetcode 题解 2. Add Two Numbers
- Leetcode题解 - 2. Add Two Numbers
- LeetCode Algorithms 2. Add Two Numbers 题解
- Nginx+Tomcat+Redis搭建SESSION集群
- json-default和struts-default区别
- 转让app后AppStore应用名称下面所有者显示问题
- IntelliJ IDEA开发Java web项目,JSP无代码提示或自动补全 问题
- storm ui查看不了任务详情
- LeetCode题解(2)--Add Two Numbers
- ubuntu16.04+xfce4截图、终端调出、锁屏的快捷键设置、去掉Alt+鼠标移动屏幕的功能、安装thunar
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- ecplise使用过程中出现的问题总结
- 99%用户不知道 搜索引擎这样用才有效率
- 文件的操作
- 全面解析IOS应用启动都做了些什么
- Jquery对动态添加进去的元素无法使用on绑定事件处理
- HUSTOJ 1366 Game 斐波那契博弈