leetcode——2
1. 题目
Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8 即342+465=807
给你两个链表代表两个非负数。数字以相反的顺序存储,每个节点包含一个单一的数字。加上这两个数并返回一个链表。
2.c++解题
//LeetCode_Add Two Numbers
//Written by zhou
//2013.11.1 /**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (l1 == NULL) return l2;
if (l2 == NULL) return l1; ListNode *resList = NULL, *pNode = NULL, *pNext = NULL; // resList头节点, pNode 每轮的末节点, pNext临时节点
ListNode *p = l1, *q = l2;
int up = 0;
while(p != NULL && q != NULL)
{
pNext = new ListNode(p->val + q->val + up);
up = pNext->val / 10; //计算进位
pNext->val = pNext->val % 10; //计算该位的数字
if (resList == NULL) //头结点为空
{
resList = pNode = pNext;
}
else //头结点不为空
{
pNode->next = pNext;
pNode = pNext;
}
p = p->next;
q = q->next;
} //处理链表l1剩余的高位
while (p != NULL)
{
pNext = new ListNode(p->val + up);
up = pNext->val / 10;
pNext->val = pNext->val % 10;
pNode->next = pNext;
pNode = pNext;
p = p->next;
} //处理链表l2剩余的高位
while (q != NULL)
{
pNext = new ListNode(q->val + up);
up = pNext->val / 10;
pNext->val = pNext->val % 10;
pNode->next = pNext;
pNode = pNext;
q = q->next;
} //如果有最高处的进位,需要增加结点存储
if (up > 0)
{
pNext = new ListNode(up);
pNode->next = pNext;
} return resList;
} };
3. python解题
3.1
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
dummy, flag = ListNode(0), 0
head = dummy
while flag or l1 or l2: //flag 进位, node临时节点, dummy最后节点
node = ListNode(flag)
if l1:
node.val += l1.val
l1 = l1.next
if l2:
node.val += l2.val
l2 = l2.next
flag = node.val / 10
node.val %= 10
head.next = node
head = head.next # head.next, head = node, node
return dummy.next
3.2
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
if not l1: return l2
if not l2: return l1
dummy = ListNode(0)
p = dummy
flag = 0
while l1 and l2:
tmp = l1.val + l2.val + flag
p.next = ListNode( tmp % 10 )
flag = tmp / 10
l1, l2, p = l1.next, l2.next, p.next
if l1:
while l1:
tmp = l1.val + flag
p.next = ListNode( tmp % 10 )
flag = tmp / 10
l1, p = l1.next, p.next
if l2:
while l2:
tmp = l2.val + flag
p.next = ListNode( tmp % 10 )
flag = tmp / 10
l2, p = l2.next, p.next
if flag == 1: p.next = ListNode(flag)
return dummy.next
4 java
public class Solution { // Definition for singly-linked list.
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
} public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ret = new ListNode(0);
ListNode cur = ret; int sum = 0;
while (true) {
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
cur.val = sum % 10;
sum /= 10;
if (l1 != null || l2 != null || sum != 0) {
cur = (cur.next = new ListNode(0));
} else {
break;
}
}
return ret;
}
}
leetcode——2的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 【service调用dao层传参的三种方式】
第一种方案:默认数组角标: service Public User selectUser(String name,String area); mapper: <select id="s ...
- java第一天--Java开发环境的搭建以及使用eclipse从头一步步创建java项目
一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可..http://www.orac ...
- HBase高可用原理与实践
前言 前段时间有套线上HBase出了点小问题,导致该套HBase集群服务停止了2个小时,从而造成使用该套HBase作为数据存储的应用也出现了服务异常.在排查问题之余,我们不禁也在思考,以后再出现类似的 ...
- PAT 1071【STL string应用】
1.单case很多清空没必要的 2.string+ char 最好用pushback 3.string +string就直接+ #include <bits/stdc++.h> using ...
- 2017-10-1 清北刷题冲刺班a.m
位运算1 (bit) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK拥有一个十进制的数N.它赋予了N一个新的意义:将N每一位都拆开来后再加起来就是N所拥 ...
- 白白的(baibaide)——树状数组套主席树+splay
题目 [题目描述] 有一个长度为 $n$ 的序列 $a_1, a_2, \dots, a_n$,一开始每个位置都是白色.如果一个区间中每个位置都是白色,则称这是一个白白的区间.如果一个白白的区间向左或 ...
- 为什么Python如此慢
Python当前人气暴涨.它在DevOps,数据科学,Web开发和安全领域均有使用. 但是在速度方面没有赢得美誉. 这里有关于Python比较其他语言如,Java, C#, Go, JavaScrip ...
- POJ1034 The dog task
题目来源:http://poj.org/problem?id=1034 题目大意: 一个猎人在遛狗.猎人的路径由一些给定的点指定.狗跟随着猎人,要与主人同时到达那些指定的点.在丛林里有一些有趣的地方, ...
- Leetcode初级算法(排序和搜索+数学篇)
合并两个有序数组 开始的时候将这道题理解错了,发现几个奇怪的测试案例后才明白这道题什么意思.本来的想法就是把nums2全部放到num1里面,然后删除重复元素.排序一下,就有了下面的代码: class ...
- "微信戴圣诞帽"的一个简易实现程序
准备安装 由于是利用别人写的人脸识别的一个库,所以需要在import之前安装好相应的环境.如果直接安装face_recognition库的时候就会直接提示缺少的相应的dlib库.而dlib库本身需要c ...