【Leetcode】Add Two Numbers
来源:互联网 发布:java 模拟get请求 编辑:程序博客网 时间:2024/06/06 03:18
题目:
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
翻译:
给你2个链表,代表2个非负整数。链表中整数的每一位数字的存储是反序的,数组的每个节点都包含一个数字。把2个非负整数相加,并且用一个链表返回。
输入: (2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 0 -> 8
分析:
由于刚好链表中数的存储是反序,也就是个位在最前面,正好方便我们从低位开始加。需要注意的几个点:
1. 传入[][],也就是2个空链表,要返回null
2. 传入[0][0],也就是2个整数是0的处理,要返回[0]
3. 传入[5][5],要新增一个节点,存储进位。也就是判断是否结束,要根据2个链表是否为空和是否有进位来判断。
Java版本:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode prev = new ListNode(0); ListNode head = prev; int carry = 0; while (l1 != null || l2 != null || carry != 0) { ListNode cur = new ListNode(0); int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry; cur.val = sum % 10; carry = sum / 10; prev.next = cur; prev = cur; l1 = (l1 == null) ? l1 : l1.next; l2 = (l2 == null) ? l2 : l2.next; } return head.next; }}
Python版本:
# Definition for singly-linked list.# 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 """ carry = 0 root = n = ListNode(0) while l1 or l2 or carry: v1 = v2 = 0 if l1: v1 = l1.val l1 = l1.next if l2: v2 = l2.val l2 = l2.next carry, val = divmod(v1+v2+carry, 10) n.next = ListNode(val) n = n.next return root.next
C++版本:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode preHead(0), *p = &preHead; int extra = 0; while (l1 || l2 || extra) { int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra; extra = sum / 10; p->next = new ListNode(sum % 10); p = p->next; l1 = l1 ? l1->next : l1; l2 = l2 ? l2->next : l2; } return preHead.next; }};
阅读全文
0 0
- LeetCode: Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- Leetcode : Add Two Numbers
- 【leetcode】Add Two Numbers
- [LeetCode]Add Two Numbers
- leetcode Add Two Numbers
- LeetCode-Add Two Numbers
- [leetcode] Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode - Add Two Numbers
- LeetCode | Add Two Numbers
- 【leetcode】Add Two Numbers
- Git常用命令总结
- wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)
- C语言-模拟实现strcat
- 集群和负载均衡
- 光纤跳线与铜线在延迟上的区别
- 【Leetcode】Add Two Numbers
- Glide使用高级技巧(解决Glide生成缓存Key问题)
- 观察者模式
- excel生成excel语句
- 线程更新UI
- anguar学习原网页
- 记一次错误“net::ERR_INCOMPLETE_CHUNKED_ENCODING”
- Android去除屏幕最上方的标签(App名字)
- 安卓开发之webview长按复制粘贴功能