-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path92.reverse-linked-list-ii-iteration.c
More file actions
81 lines (67 loc) · 1.67 KB
/
92.reverse-linked-list-ii-iteration.c
File metadata and controls
81 lines (67 loc) · 1.67 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdlib.h>
#include <stdio.h>
// Definition for singly-linked list.
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
if (m == n)
return head;
struct ListNode *prev = NULL, *next = NULL, *p = head;
// left is the m point left point, right is the m point
// left->next should be the n point, right->next should be the n->next point
struct ListNode *left = NULL, *right = NULL;
int count = 1;
while (p != NULL && count <= n)
{
next = p->next;
if (count == m)
{
left = prev;
right = p;
}
if (count >= m)
p->next = prev;
if (count == n)
{
if (left != NULL)
left->next = p;
right->next = next;
// if reverse from the original head, the new head should locate on the n point
// otherwise the head should be the original head
if (m == 1)
head = p;
}
prev = p;
p = next;
count++;
}
return head;
}
int main(int argc, char const *argv[])
{
struct ListNode *list = malloc(sizeof(struct ListNode));
struct ListNode *p = list;
for (size_t i = 1; i < 6; i++)
{
p->val = i;
if (i == 5)
break;
p->next = malloc(sizeof(struct ListNode));
p = p->next;
}
p->next = NULL;
list = reverseBetween(list, 1, 1);
struct ListNode *prev;
while (list != NULL)
{
prev = list;
printf("%d ", list->val);
list = list->next;
free(prev);
}
return 0;
}