-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_reverse.c
executable file
·58 lines (54 loc) · 1.7 KB
/
list_reverse.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_reverse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/22 19:32:42 by akharrou #+# #+# */
/* Updated: 2019/05/19 10:33:25 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_reverse -- reverse a list in place.
**
** SYNOPSIS
** #include <../libft.h>
**
** int
** list_reverse(t_list **head);
**
** PARAMETERS
**
** t_list **head Pointer to a pointer
** to the first element
** of a list.
**
** DESCRIPTION
** Reverses a list in place.
**
** RETURN VALUES
** If successful returns 0; otherwise -1.
*/
#include "../Includes/list.h"
int list_reverse(t_list **head)
{
t_list *after_last;
t_list *last;
int size;
if (head && (*head))
{
size = list_count(*head);
last = list_last_elem(*head);
while (size-- > 1)
{
after_last = (*head);
(*head) = (*head)->next;
after_last->next = last->next;
last->next = after_last;
}
head = &last;
}
return (-1);
}