leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 1e6a5a481a66d788a6afb603324e805301aed129
parent ef88eba3d2a3e81f612772dfdd0cd13cd61f3d53
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue,  7 May 2024 13:30:40 +0200

Daily Problem

Diffstat:
AProblems/2816.cpp | 29+++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/Problems/2816.cpp b/Problems/2816.cpp @@ -0,0 +1,29 @@ +class Solution { + static ListNode *rev(ListNode *list) { + ListNode *prev = nullptr, *next; + while (list) { + next = list->next; + list->next = prev; + prev = list; + list = next; + } + return prev; + } + + public: + ListNode *doubleIt(ListNode *head) const { + head = rev(head); + + ListNode *l = nullptr; + int carry = 0; + for (ListNode *p = head; p; l = p, p = p->next) { + const int val = p->val * 2 + carry; + p->val = val % 10; + carry = val / 10; + } + + if (carry) l->next = new ListNode(carry); + + return rev(head); + } +}; diff --git a/README.md b/README.md @@ -1189,6 +1189,7 @@ for solving problems. | 2785 | Medium | [Sort Vowels in a String](Problems/2785.cpp) | | 2799 | Medium | [Count Complete Subarrays in an Array](Problems/2799.cpp) | | 2807 | Medium | [Insert Greatest Common Divisors in Linked List](Problems/2807.cpp) | +| 2816 | Medium | [Double a Number Represented as a Linked List](Problems/2816.cpp) | | 2840 | Medium | [Check if Strings Can be Made Equal With Operations II](Problems/2840.cpp) | | 2849 | Medium | [Determine if a Cell Is Reachable at a Given Time](Problems/2849.cpp) | | 2856 | Medium | [Minimum Array Length After Pair Removals](Problems/2856.cpp) |