题目地址:Plus One Linked List - LeetCode


Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

Example :

Input: [1,2,3]
Output: [1,2,4]

这道题目的意思也很简单,就是链表加1.

Python解法如下:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution:def plusOne(self, head: ListNode) -> ListNode:l = []while head != None:l.append(head)head = head.nexti = len(l)-1while i >= 0:l[i].val += 1if l[i].val >= 10:l[i].val -= 10i -= 1else:breakif i == -1:root = ListNode(x=1)root.next = l[0]return rootelse:return l[0]

时间复杂度为O(n),空间复杂度为O(1)。

C++解法如下:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:ListNode *plusOne(ListNode *head) {if (head == nullptr) {return nullptr;}vector<ListNode *> l;while (head != nullptr) {l.push_back(head);head = head->next;}int i = l.size() - 1;while (i >= 0) {l[i]->val++;if (l[i]->val >= 10) {l[i]->val -= 10;i--;} else {break;}}if (i == -1) {ListNode *root = new ListNode(1);root->next = l[0];return root;} else {return l[0];}}
};

LeetCode 369. Plus One Linked List--链表--C++,Python解法相关推荐

  1. LeetCode Notes_#206 Reverse Linked List(C++,Python)

    LeetCode Notes_#206 Reverse Linked List(C++,Python) LeetCode Linked List  Contents 题目 思路 思考 解答 C++ P ...

  2. LeetCode 148. Sort List--面试算法题--C++,Python解法

    LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...

  3. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  4. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  5. LeetCode 152. Maximum Product Subarray--动态规划--C++,Python解法

    题目地址:Maximum Product Subarray - LeetCode Given an integer array nums, find the contiguous subarray w ...

  6. LeetCode 93. Restore IP Addresses--面试算法题--Python解法

    题目地址:Restore IP Addresses - LeetCode Given a string containing only digits, restore it by returning ...

  7. LeetCode 316. Remove Duplicate Letters--贪心--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  8. LeetCode - 题 70 跳台阶 - 具体思路与python解法

    LeetCode 题目 假设你正在爬楼梯.需要 n 阶你才能到达楼顶.每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 剑指offer 题目 青蛙 ...

  9. LeetCode 3. Longest Substring Without Repeating Characters-- c++,java,python解法

    题目地址: Given a string, find the length of the longest substring without repeating characters. Example ...

最新文章

  1. iOS 宏(define)与常量(const)的正确使用
  2. NeurIPS 2020 | FixMatch:通过图像增强就能实现半监督学习
  3. leetcode127. 单词接龙(bfs)
  4. 如何做到每天写代码?
  5. Linux强行取消挂载
  6. 知识蒸馏,中文文本分类,教师模型BERT,学生模型biLSTM
  7. Highway Networks
  8. Apple Magic Mouse 卡顿的问题
  9. uniapp对接阿里云OSS前端直传
  10. JAVA MONGODB 查询时间段
  11. 消息轨迹、ACL 与多副本搭建
  12. springboot日志配置logback-spring.xml
  13. 2、mysql基本操作中
  14. rtp server
  15. UVa 1471 Defense Lines - 线段树 - 离散化
  16. 如何给自己制订一个“可实现的”新年目标?
  17. Nuxt3接入51la等网站统计
  18. Impala 技术点梳理
  19. m32f1 68脚 sch
  20. 基于Python生成OPENSEES结构动画

热门文章

  1. python使用matplotlib, seaborn画图时候的数据加载
  2. JavaScript中的constructor属性
  3. convert-----Linux中的图片处理工具
  4. 男女洗澡前后区别,太形象了!
  5. R语言|PLS_DA分析绘图示例
  6. 做科研已经很辛苦了,为什么还要做PPT汇报?
  7. Nature综述:肠道微生物在人类代谢健康与疾病中的作用
  8. Nature综述:微生物的衰老与寿命
  9. Cell二连发 | 广东CDC/耶鲁大学利用纳米孔测序揭示中/美新冠病毒基因组流行病学传播规律...
  10. 专家点评Nat Micro | 朱永群组首次发现特异地切割线性泛素链的全新去泛素化酶...