Leetcode 369. Plus One Linked List (Medium) (cpp)
来源:互联网 发布:java整型转16进制 编辑:程序博客网 时间:2024/05/16 10:22
Leetcode 369. Plus One Linked List (Medium) (cpp)
Tag: Linked List
Difficulty: Medium
这是一道locked题目,给评论个“赞”呗?
/*369. Plus One Linked List (Medium)Given a non-negative number represented as a singly linked list of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.Example:Input:1->2->3Output:1->2->4*//** * 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 == NULL) { return head; } ListNode *newhead = reverse(head); int carry = 1; for (ListNode *p = newhead; p != NULL && carry != 0; p = p -> next) { int value = p -> val + carry; p -> val = value % 10; carry = value / 10; } head = reverse(newhead); if (carry > 0) { ListNode *p = new ListNode(1); p -> next = head; return p; } return head; }private: ListNode* reverse(ListNode* head) { if (head == NULL || head -> next == NULL) { return head; } ListNode *pre = NULL; while (head != NULL) { ListNode *post = head -> next; head -> next = pre; pre = head; head = post; } return pre; }};
0 0
- Leetcode 369. Plus One Linked List (Medium) (cpp)
- LeetCode 369. Plus One Linked List
- [leetCode] 369. Plus One Linked List
- [leetcode] 369. Plus One Linked List 解题报告
- LeetCode 369. Plus One Linked List(链表加1)
- [Leetcode] 369. Plus One Linked List 解题报告
- Plus One Linked List
- Leetcode 142. Linked List Cycle II (Medium) (cpp)
- Leetcode 92. Reverse Linked List II (Medium) (cpp)
- Leetcode 328. Odd Even Linked List (Medium) (cpp)
- leetcode 日经贴,Cpp code -Plus One
- Leetcode 66. Plus One (Easy) (cpp)
- Leetcode 143. Reorder List (Medium) (cpp)
- Leetcode 61. Rotate List (Medium) (cpp)
- Leetcode 86. Partition List (Medium) (cpp)
- Leetcode 147. Insertion Sort List (Medium) (cpp)
- Leetcode 148. Sort List (Medium) (cpp)
- [Leetcode 92, medium] Reverse Linked-list II
- review of machine learning 1
- [网站搭建] 阿里云搭建个人网站及域名绑定
- Medium 213题 House Robber II
- 第六章 个人优化vim第三步 :主题colorscheme
- 设计模式 - Singleton in Java
- Leetcode 369. Plus One Linked List (Medium) (cpp)
- touchpanel驱动
- Easy 205题 Isomorphic Strings
- 198. House Robber
- Java中Map按value值进行排序
- TortoiseSVN提交commit提示Unable to create pristine install stream.系统找不到指定的路径
- 200. Number of Islands
- Leetcode 4. Median of Two Sorted Arrays (Hard) (cpp)
- swift项目:微博