-
Notifications
You must be signed in to change notification settings - Fork 1
/
0206-reverse-linked-list.c
49 lines (48 loc) · 1.07 KB
/
0206-reverse-linked-list.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/*
struct ListNode* reverseList(struct ListNode* head) {
if(NULL == head)
return NULL;
struct ListNode* p = head;
struct ListNode* value = NULL;
struct ListNode* ret = value;
while(NULL != p)
{
value =(struct ListNode*) malloc(sizeof(struct ListNode));
if(NULL == value)
{
return NULL;
}
memset(value,0,sizeof(struct ListNode));
value->val = p->val;
value->next = ret;
ret = value;
p = p->next;
}
return ret;
}*/
struct ListNode* reverseList(struct ListNode* head) {
if(NULL == head)
return NULL;
struct ListNode* pre = NULL;
struct ListNode* cur = head;
struct ListNode* nextNode = head->next;
while(NULL != cur)
{
cur->next = pre;
pre = cur;
cur = nextNode;
if(NULL != nextNode)
{
nextNode = nextNode->next;
}
}
head = pre;
return head;
}