-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseList.c
67 lines (62 loc) · 1.43 KB
/
reverseList.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
57
58
59
60
61
62
63
64
65
66
//https://leetcode.com/problems/reverse-linked-list/
//
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
// iterative version
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode *next, *pre=NULL, *cur=head;
while(cur != NULL){ // cur 表示当前要处理的节点
next = cur->next;
cur->next = pre;
pre=cur;
cur=next;
}
return pre;
}
// recursive version
struct ListNode* reverseList2(struct ListNode* head) {
struct ListNode *next;
if(head == NULL)
return NULL;
next = head->next;
if(next == NULL)
return head;
else{
head->next = NULL;
struct ListNode *newhead = reverseList2(next);
next->next = head;
return newhead;
}
}
void print(struct ListNode * node){
while(node != NULL){
printf("%d ", node->val);
node=node->next;
}
printf("\n");
}
int main(){
struct ListNode *list, *node1, *node2;
node1 = (struct ListNode *)malloc(sizeof(struct ListNode));
node2 = (struct ListNode *)malloc(sizeof(struct ListNode));
node1->val=1;
node1->next = node2;
node2->val=2;
node2->next=NULL;
list=node1;
print(list);
// print(reverseList(list));
print(reverseList2(list));
return 0;
}