-
Notifications
You must be signed in to change notification settings - Fork 8
/
Solution.cpp
71 lines (64 loc) · 1.4 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/**
* 27 / 27 test cases passed.
* Status: Accepted
* Runtime: 8 ms
*/
class Solution1 {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL) return head;
ListNode *h, *p, *q;
h = new ListNode(-1);
h->next = NULL;
p = head;
q = head->next;
while (p) {
p->next = h->next;
h->next = p;
p = q;
q = q == NULL ? NULL : q->next;
}
return h->next;
}
};
/**
* 27 / 27 test cases passed.
* Status: Accepted
* Runtime: 16 ms
*/
class Solution2 {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL) return head;
ListNode* node = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return node;
}
};
int main () {
ListNode node1(1);
ListNode node2(2);
ListNode node3(3);
ListNode node4(4);
ListNode node5(5);
node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5;
node5.next = NULL;
ListNode* head = Solution2().reverseList(&node1);
while (head) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
return 0;
}