Skip to content

Commit efd8033

Browse files
committed
206. Clean solution
1 parent 503fbef commit efd8033

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

main.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,40 @@
1010
using namespace std;
1111

1212

13+
struct ListNode {
14+
int val;
15+
ListNode *next;
16+
ListNode() : val(0), next(nullptr) {}
17+
ListNode(int x) : val(x), next(nullptr) {}
18+
ListNode(int x, ListNode *next) : val(x), next(next) {}
19+
};
20+
1321
class Solution {
1422
public:
15-
bool isPowerOfTwo(int n) {
16-
if (n == -(1L << 31)) return false;
17-
return __builtin_popcount(n) == 1;
23+
ListNode* reverseList(ListNode* head) {
24+
if (head == nullptr) {
25+
return head;
26+
}
27+
28+
ListNode* next = nullptr;
29+
while (head != nullptr) {
30+
auto tmp = head->next;
31+
head->next = next;
32+
next = head;
33+
head = tmp;
34+
}
35+
36+
return next;
1837
}
1938
};
2039

2140

2241
int main() {
42+
auto r = Solution().reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4)))));
43+
while (r != nullptr) {
44+
cout << r->val << " ";
45+
r = r->next;
46+
}
47+
2348
return 0;
2449
}

0 commit comments

Comments
 (0)