-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverselinkedlist.c
59 lines (51 loc) · 1.16 KB
/
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
57
58
59
#include <stdio.h>
#include <stdlib.h>
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL)
{
return NULL;
}
if (head->next == NULL)
{
return head;
}
struct ListNode *current = head;
struct ListNode *currentnext = head->next;
struct ListNode* temp = NULL;
while (currentnext != NULL)
{
temp = current;
// printf("temp = 0x%x, val=%d\n", temp, temp->val);
current = currentnext;
// printf("current = 0x%x, val=%d\n", current, current->val);
currentnext = currentnext->next;
current->next = temp;
// printf("current = 0x%x, curent->next=0x%x\n", current, current->next);
if (temp == head)
{
temp->next = NULL;
}
}
// printf("current = 0x%x, val=%d\n", temp, temp->val);
return current;
}
void main()
{
struct ListNode Node1;
struct ListNode Node2;
struct ListNode Node3;
struct ListNode Node4;
struct ListNode Node5;
}