Skip to content

Commit 8e1b2bd

Browse files
authored
Create double-a-number-represented-as-a-linked-list.cpp
1 parent c303075 commit 8e1b2bd

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// linked list
5+
class Solution {
6+
public:
7+
ListNode* doubleIt(ListNode* head) {
8+
if (head->val >= 5) {
9+
head = new ListNode(0, head);
10+
}
11+
for (auto curr = head; curr; curr = curr->next) {
12+
curr->val = (curr->val * 2) % 10;
13+
if (curr->next && curr->next->val >= 5) {
14+
++curr->val;
15+
}
16+
}
17+
return head;
18+
}
19+
};

0 commit comments

Comments
 (0)