modified: main.cpp
modified: offer06.cpp
This commit is contained in:
32
main.cpp
32
main.cpp
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* @Description:
|
||||||
|
* @Version: 1.0
|
||||||
|
* @Autor: wxchen
|
||||||
|
* @Date: 2022-11-21 15:27:10
|
||||||
|
* @LastEditTime: 2022-11-21 20:28:56
|
||||||
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -6,16 +13,35 @@ using namespace std;
|
|||||||
struct ListNode {
|
struct ListNode {
|
||||||
int val;
|
int val;
|
||||||
ListNode* next;
|
ListNode* next;
|
||||||
ListNode(int x) : val(x), next(NULL) {}
|
// ListNode(int x) : val(x), next(NULL) {} //
|
||||||
|
ListNode(int x, ListNode* n = NULL) : val(x), next(n) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
vector<int> reversePrint(ListNode* head) {
|
vector<int> reversePrint(ListNode* head) {
|
||||||
|
reverse(head);
|
||||||
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<int> result;
|
||||||
|
void reverse(ListNode* head) {
|
||||||
|
if (head != nullptr) {
|
||||||
|
if (head->next != nullptr) {
|
||||||
|
reversePrint(head->next);
|
||||||
|
}
|
||||||
|
result.push_back(head->val);
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int, char**) {
|
int main(int, char**) {
|
||||||
std::cout << "Hello, world!\n";
|
|
||||||
|
// ListNode* head = new ListNode(1);
|
||||||
|
// head -> next = new ListNode(3);
|
||||||
|
// head -> next -> next = new ListNode(2);
|
||||||
|
|
||||||
|
Solution s;
|
||||||
|
std::cout << s.reversePrint(head)[0] << s.reversePrint(head)[1] << s.reversePrint(head)[2] << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
47
offer06.cpp
47
offer06.cpp
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* @Description:
|
||||||
|
* @Version: 1.0
|
||||||
|
* @Autor: wxchen
|
||||||
|
* @Date: 2022-11-21 15:27:10
|
||||||
|
* @LastEditTime: 2022-11-21 21:06:07
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode* next;
|
||||||
|
// ListNode(int x) : val(x), next(NULL) {} //
|
||||||
|
ListNode(int x, ListNode* n = NULL) : val(x), next(n) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> reversePrint(ListNode* head) {
|
||||||
|
reverse(head);
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<int> result;
|
||||||
|
void reverse(ListNode* head) {
|
||||||
|
if (head != nullptr) {
|
||||||
|
if (head->next != nullptr) {
|
||||||
|
reversePrint(head->next);
|
||||||
|
}
|
||||||
|
result.push_back(head->val);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int, char**) {
|
||||||
|
|
||||||
|
// ListNode* head = new ListNode(1);
|
||||||
|
// head -> next = new ListNode(3);
|
||||||
|
// head -> next -> next = new ListNode(2);
|
||||||
|
|
||||||
|
Solution s;
|
||||||
|
std::cout << s.reversePrint(head)[0] << s.reversePrint(head)[1] << s.reversePrint(head)[2] << std::endl;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user