-
Notifications
You must be signed in to change notification settings - Fork 9
/
reverseLinkedList.cpp
79 lines (70 loc) · 1.38 KB
/
reverseLinkedList.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
72
73
74
75
76
77
78
/**
* leetcode: reverse linked list
* easy
*/
#include <cstdio>
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* createList(int* values, int size) {
ListNode* head = NULL;
ListNode* tail = NULL;
for (int i=0; i<size; ++i) {
ListNode* p = new ListNode(values[i]);
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}
return head;
}
void destroyList(ListNode* head) {
ListNode* p = head;
while (p != NULL) {
ListNode* temp = p;
p = p->next;
delete temp;
}
}
void printList(ListNode* head) {
ListNode* p = head;
printf("[");
while (p->next != NULL) {
printf("%d,", p->val);
p = p->next;
}
printf("%d]\n", p->val);
}
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL) {
return NULL;
}
}
private:
ListNode* method1Interative(ListNode* head) {
}
};
int main() {
const int N = 5;
int a[N] = {1,2,3,4,5};
Solution sln;
for (int i=0; i<10; ++i) {
ListNode* head = createList(a, N);
printf("%d-----\n", i);
printList(head);
ListNode* newHead = sln.rotateRight(head, i);
printList(newHead);
destroyList(newHead);
}
return 0;
}