-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseLinkedList.c
56 lines (45 loc) · 979 Bytes
/
ReverseLinkedList.c
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
#include <stdlib.h>
#include "ReverseLinkedList.h"
Node* ReverseLinkedList(Node* node) {
Node* cur = node;
Node* pre = NULL;
Node* temp = NULL;
while(cur) {
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
Node* CreateLinkedList(int* array, int length) {
int i = 0;
Node* cur = NULL;
Node* temp = NULL;
Node* head = NULL;
for(i = 0; i < length; i++) {
temp = malloc(sizeof(Node));
if (temp == NULL) {
DestoryLinkedList(head);
return NULL;
}
temp->val = array[i];
temp->next = NULL;
if(cur)
cur->next = temp;
else
head = temp;
cur = temp;
}
return head;
}
void DestoryLinkedList(Node* node) {
Node* cur = node;
Node* temp = NULL;
while(cur) {
temp = cur->next;
free(cur);
cur = temp;
}
return;
}